From 78745cb7570ec5b9b03fca9473c8b3f6ffa51f58 Mon Sep 17 00:00:00 2001 From: Damini2004 Date: Sun, 23 Jun 2024 14:22:47 +0530 Subject: [PATCH 1/4] Commit 1 --- .../0101-0200/0111-Maximum-Number-of-coins.md | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md diff --git a/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md b/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md new file mode 100644 index 000000000..fc9fe31f5 --- /dev/null +++ b/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md @@ -0,0 +1,118 @@ +--- +id: maximum-number-of-coins +title: Maximum Number of coins (Geeks for Geeks) +sidebar_label: Maximum Number of coins Problem +tags: + - Intermediate + - Geeks for Geeks + - CPP + - Python + - DSA +description: "This is a solution to the Maximum Number of coins Problem on Geeks for Geeks." +--- + +## Problem Description + +We have been given N balloons, each with a number of coins associated with it. On bursting a balloon i, the number of coins gained is equal to A[i-1]*A[i]*A[i+1]. +Also, balloons i-1 and i+1 now become adjacent. Find the maximum possible profit earned after bursting all the balloons. Assume an extra 1 at each boundary. + +## Examples + +**Example:** + +Consider the following graph: + +**Input:** ` N=2 , a[]={5, 10}` +**Output:** `60` +**Explanation:** First Burst `5`, Coins = `1*5*10` , Then burst `10`, Coins+=` 1*10*1` , Total = `60` + +**Example:** + +**Input:** `N=4 , a[] = {3,1,5,8}` +**Output:** 167 +**Explanation:** +`nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []` +`coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167.` + +## Your Task + +You don't need to read input or print anything. Your task is to complete the function maxCoins() which takes the array arr[], its size N, and returns the maximum number of coins that can be collected. + +Expected Time Complexity: $O(N^3)$ +Expected Space Complexity: $O(N^2)$ + +## Constraints +- 1 <= N <= 400 +- 0 <= a[i] <= 100 + +## Problem Explanation + +Here's the step-by-step breakdown of the Maximum Number of coins process: + +There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: + +**Step 1:** In each step, you will choose any 3 piles of coins (not necessarily consecutive). +**Step 2 :**Of your choice, Alice will pick the pile with the maximum number of coins. +**Step 3 :**You will pick the next pile with the maximum number of coins. +**Step 4 :**Your friend Bob will pick the last pile. +**Step 5 :**Repeat until there are no more piles of coins. + +Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins that you can have. + +### Code Implementation + + + + + ```python + +class Solution: + def maxCoins(self, piles: List[int]) -> int: + piles.sort() + return sum(piles[-2:(len(piles)//3)-1:-2]) + + ``` + + + + ```cpp + class Solution { +public: + + int maxCoins(vector& piles) { + // sort the piles in non-increasing order + sort(piles.begin(), piles.end(), greater()); + + // greedy using two-pointers + int sum = 0; + for (int l = 0, r = piles.size() - 1; l < r; l += 2, r--) { + sum += piles[l + 1]; + } + return sum; + } +}; + ``` + + + + +## Solution Logic + - Sort the piles in non-increasing order + - Greedy using two-pointers + + +## Time Complexity + +$$O(N^3)$$. + +## Space Complexity + +$$O(N^2)$$. + +## Resources + +- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/problems/maximum-number-of-coins--170647/1) +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/description/) +- **Author's Geeks for Geeks Profile:** | [DaminiChachane](https://leetcode.com/u/divcxl15/) | + +This format ensures that all necessary details about the problem and its solution are clearly presented and easy to follow. \ No newline at end of file From cb25e34639ef7e031a042cdde9e1913ae91fd3f8 Mon Sep 17 00:00:00 2001 From: Damini Chachane <119414762+Damini2004@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:41:29 +0530 Subject: [PATCH 2/4] Update 0111-Maximum-Number-of-coins.md --- .../Hard/0101-0200/0111-Maximum-Number-of-coins.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md b/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md index fc9fe31f5..11bfbb4a3 100644 --- a/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md +++ b/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md @@ -42,8 +42,8 @@ Expected Time Complexity: $O(N^3)$ Expected Space Complexity: $O(N^2)$ ## Constraints -- 1 <= N <= 400 -- 0 <= a[i] <= 100 +- `1 <= N <= 400` +- `0 <= a[i] <= 100` ## Problem Explanation @@ -115,4 +115,4 @@ $$O(N^2)$$. - **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/description/) - **Author's Geeks for Geeks Profile:** | [DaminiChachane](https://leetcode.com/u/divcxl15/) | -This format ensures that all necessary details about the problem and its solution are clearly presented and easy to follow. \ No newline at end of file +This format ensures that all necessary details about the problem and its solution are clearly presented and easy to follow. From 0a7ff130509ec427219be31ad8225ad33256bc3d Mon Sep 17 00:00:00 2001 From: Ajay Dhangar <99037494+Ajay-Dhangar@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:50:15 +0530 Subject: [PATCH 3/4] Update 0111-Maximum-Number-of-coins.md --- .../Hard/0101-0200/0111-Maximum-Number-of-coins.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md b/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md index 11bfbb4a3..53dc8fe27 100644 --- a/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md +++ b/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md @@ -1,5 +1,5 @@ --- -id: maximum-number-of-coins +id: maximum-number-of-coin title: Maximum Number of coins (Geeks for Geeks) sidebar_label: Maximum Number of coins Problem tags: From bcb1617ab8141eae386a99e3af178f47ed4ed087 Mon Sep 17 00:00:00 2001 From: Ajay Dhangar <99037494+Ajay-Dhangar@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:51:34 +0530 Subject: [PATCH 4/4] Update and rename 0111-Maximum-Number-of-coins.md to 111-Maximum-Number-of-coins.md --- ...aximum-Number-of-coins.md => 111-Maximum-Number-of-coins.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename dsa-solutions/gfg-solutions/Hard/0101-0200/{0111-Maximum-Number-of-coins.md => 111-Maximum-Number-of-coins.md} (99%) diff --git a/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md b/dsa-solutions/gfg-solutions/Hard/0101-0200/111-Maximum-Number-of-coins.md similarity index 99% rename from dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md rename to dsa-solutions/gfg-solutions/Hard/0101-0200/111-Maximum-Number-of-coins.md index 53dc8fe27..472aa1af7 100644 --- a/dsa-solutions/gfg-solutions/Hard/0101-0200/0111-Maximum-Number-of-coins.md +++ b/dsa-solutions/gfg-solutions/Hard/0101-0200/111-Maximum-Number-of-coins.md @@ -1,5 +1,5 @@ --- -id: maximum-number-of-coin +id: 111-maximum-number-of-coins title: Maximum Number of coins (Geeks for Geeks) sidebar_label: Maximum Number of coins Problem tags: