Leetcode 1561: Maximum Number of Coins You Can Get

grid47
grid47
Exploring patterns and algorithms
Jun 3, 2024 7 min read

You and two friends are given 3n piles of coins, and in each step, three piles are chosen. Alice always picks the pile with the most coins, you pick the second largest pile, and your friend Bob picks the remaining pile. Repeat this process until all piles are picked. Your goal is to maximize the total number of coins you can collect.
Problem
Approach
Steps
Complexity
Input: The input consists of an array of integers, where each integer represents the number of coins in a pile. The length of the array is 3n, where n is a positive integer.
Example: Input: piles = [1, 3, 2, 7, 5, 4, 6, 8, 9]
Constraints:
• 3 <= piles.length <= 10^5
• piles.length % 3 == 0
• 1 <= piles[i] <= 10^4
Output: Return the maximum number of coins you can collect.
Example: Output: 18
Constraints:
Goal: Maximize the coins you collect by strategically selecting piles in each round, ensuring you pick the second largest pile in each triplet.
Steps:
• Sort the piles in descending order to prioritize larger piles for Alice.
• Pick the second largest pile for yourself in each triplet.
• Repeat the process until all piles are picked, keeping track of your total.
Goal: The input array will have a size of 3n, with each pile having a number of coins between 1 and 10,000.
Steps:
• 3 <= piles.length <= 10^5
• piles.length % 3 == 0
• 1 <= piles[i] <= 10^4
Assumptions:
• The input array will always have a size that is a multiple of 3.
• The number of coins in each pile is a positive integer.
Input: Example 1: piles = [1, 3, 2, 7, 5, 4, 6, 8, 9]
Explanation: In this case, the optimal choices would be: (9, 8, 7), (6, 5, 4), and (3, 2, 1). You would get 8 + 6 = 14 coins.

Input: Example 2: piles = [1, 2, 3, 4, 5, 6]
Explanation: In this case, the optimal choices would be: (6, 5, 4), (3, 2, 1). You would get 5 + 2 = 7 coins.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus