Leetcode 1753: Maximum Score From Removing Stones

grid47
grid47
Exploring patterns and algorithms
May 15, 2024 5 min read

You are given three piles of stones, with sizes a, b, and c respectively. In each turn, you can choose two different non-empty piles, remove one stone from each, and add 1 point to your score. The game stops when there are fewer than two non-empty piles left, meaning no more valid moves are available. Your task is to return the maximum score you can achieve.
Problem
Approach
Steps
Complexity
Input: You are given three integers, `a`, `b`, and `c`, representing the sizes of the three piles.
Example: Input: a = 3, b = 5, c = 7
Constraints:
• 1 <= a, b, c <= 10^5
Output: Return the maximum score that can be achieved in the game.
Example: Output: 7
Constraints:
• The returned score is a non-negative integer.
Goal: To find the maximum score, we need to perform moves in an optimal way by always selecting the two largest piles.
Steps:
• 1. Sort the piles so that the largest pile is always chosen first.
• 2. Continuously remove stones from the two largest piles until fewer than two piles remain non-empty.
• 3. Keep a count of each move to compute the score.
Goal: Ensure that the solution works efficiently for large input sizes.
Steps:
• The solution should be optimized for up to 10^5 elements in the piles.
Assumptions:
• There are always at least one stone in each pile initially.
Input: Input: a = 3, b = 5, c = 7
Explanation: Starting with piles (3, 5, 7), the optimal sequence of moves is to take stones from the largest two piles (5, 7) for 5 turns, then from the remaining two piles (3, 6) for 3 turns. This results in a total of 7 points.

Input: Input: a = 2, b = 4, c = 6
Explanation: With piles (2, 4, 6), one optimal sequence is to take from the 1st and 3rd piles for 2 turns, then from the 2nd and 3rd piles for 4 turns. This gives a total score of 6.

Link to LeetCode Lab


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