|
| 1 | +--- |
| 2 | +id: 111-maximum-number-of-coins |
| 3 | +title: Maximum Number of coins (Geeks for Geeks) |
| 4 | +sidebar_label: Maximum Number of coins Problem |
| 5 | +tags: |
| 6 | + - Intermediate |
| 7 | + - Geeks for Geeks |
| 8 | + - CPP |
| 9 | + - Python |
| 10 | + - DSA |
| 11 | +description: "This is a solution to the Maximum Number of coins Problem on Geeks for Geeks." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +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]. |
| 17 | +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. |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +**Example:** |
| 22 | + |
| 23 | +Consider the following graph: |
| 24 | + |
| 25 | +**Input:** ` N=2 , a[]={5, 10}` |
| 26 | +**Output:** `60` |
| 27 | +**Explanation:** First Burst `5`, Coins = `1*5*10` , Then burst `10`, Coins+=` 1*10*1` , Total = `60` |
| 28 | + |
| 29 | +**Example:** |
| 30 | + |
| 31 | +**Input:** `N=4 , a[] = {3,1,5,8}` |
| 32 | +**Output:** 167 |
| 33 | +**Explanation:** |
| 34 | +`nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []` |
| 35 | +`coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167.` |
| 36 | + |
| 37 | +## Your Task |
| 38 | + |
| 39 | +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. |
| 40 | + |
| 41 | +Expected Time Complexity: $O(N^3)$ |
| 42 | +Expected Space Complexity: $O(N^2)$ |
| 43 | + |
| 44 | +## Constraints |
| 45 | +- `1 <= N <= 400` |
| 46 | +- `0 <= a[i] <= 100` |
| 47 | + |
| 48 | +## Problem Explanation |
| 49 | + |
| 50 | +Here's the step-by-step breakdown of the Maximum Number of coins process: |
| 51 | + |
| 52 | +There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: |
| 53 | + |
| 54 | +**Step 1:** In each step, you will choose any 3 piles of coins (not necessarily consecutive). |
| 55 | +**Step 2 :**Of your choice, Alice will pick the pile with the maximum number of coins. |
| 56 | +**Step 3 :**You will pick the next pile with the maximum number of coins. |
| 57 | +**Step 4 :**Your friend Bob will pick the last pile. |
| 58 | +**Step 5 :**Repeat until there are no more piles of coins. |
| 59 | + |
| 60 | +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. |
| 61 | + |
| 62 | +### Code Implementation |
| 63 | + |
| 64 | +<Tabs> |
| 65 | + <TabItem value="Python" label="Python" default> |
| 66 | + <SolutionAuthor name="@ngmuraqrdd"/> |
| 67 | + ```python |
| 68 | + |
| 69 | +class Solution: |
| 70 | + def maxCoins(self, piles: List[int]) -> int: |
| 71 | + piles.sort() |
| 72 | + return sum(piles[-2:(len(piles)//3)-1:-2]) |
| 73 | + |
| 74 | + ``` |
| 75 | + </TabItem> |
| 76 | + <TabItem value="C++" label="C++"> |
| 77 | + <SolutionAuthor name="@ngmuraqrdd"/> |
| 78 | + ```cpp |
| 79 | + class Solution { |
| 80 | +public: |
| 81 | + |
| 82 | + int maxCoins(vector<int>& piles) { |
| 83 | + // sort the piles in non-increasing order |
| 84 | + sort(piles.begin(), piles.end(), greater<int>()); |
| 85 | + |
| 86 | + // greedy using two-pointers |
| 87 | + int sum = 0; |
| 88 | + for (int l = 0, r = piles.size() - 1; l < r; l += 2, r--) { |
| 89 | + sum += piles[l + 1]; |
| 90 | + } |
| 91 | + return sum; |
| 92 | + } |
| 93 | +}; |
| 94 | + ``` |
| 95 | + |
| 96 | + </TabItem> |
| 97 | +</Tabs> |
| 98 | + |
| 99 | +## Solution Logic |
| 100 | + - Sort the piles in non-increasing order |
| 101 | + - Greedy using two-pointers |
| 102 | + |
| 103 | + |
| 104 | +## Time Complexity |
| 105 | + |
| 106 | +$$O(N^3)$$. |
| 107 | + |
| 108 | +## Space Complexity |
| 109 | + |
| 110 | +$$O(N^2)$$. |
| 111 | + |
| 112 | +## Resources |
| 113 | + |
| 114 | +- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/problems/maximum-number-of-coins--170647/1) |
| 115 | +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/description/) |
| 116 | +- **Author's Geeks for Geeks Profile:** | [DaminiChachane](https://leetcode.com/u/divcxl15/) | |
| 117 | + |
| 118 | +This format ensures that all necessary details about the problem and its solution are clearly presented and easy to follow. |
0 commit comments