1753. Maximum Score From Removing Stones LeetCode Solution

In this guide, you will get 1753. Maximum Score From Removing Stones LeetCode Solution with the best time and space complexity. The solution to Maximum Score From Removing Stones problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Score From Removing Stones solution in C++
  4. Maximum Score From Removing Stones solution in Java
  5. Maximum Score From Removing Stones solution in Python
  6. Additional Resources
1753. Maximum Score From Removing Stones LeetCode Solution image

Problem Statement of Maximum Score From Removing Stones

You are playing a solitaire game with three piles of stones of sizes a​​​​​​, b,​​​​​​ and c​​​​​​ respectively. Each turn you choose two different non-empty piles, take one stone from each, and add 1 point to your score. The game stops when there are fewer than two non-empty piles (meaning there are no more available moves).
Given three integers a​​​​​, b,​​​​​ and c​​​​​, return the maximum score you can get.

Example 1:

Input: a = 2, b = 4, c = 6
Output: 6
Explanation: The starting state is (2, 4, 6). One optimal set of moves is:
– Take from 1st and 3rd piles, state is now (1, 4, 5)
– Take from 1st and 3rd piles, state is now (0, 4, 4)
– Take from 2nd and 3rd piles, state is now (0, 3, 3)
– Take from 2nd and 3rd piles, state is now (0, 2, 2)
– Take from 2nd and 3rd piles, state is now (0, 1, 1)
– Take from 2nd and 3rd piles, state is now (0, 0, 0)
There are fewer than two non-empty piles, so the game ends. Total: 6 points.

Example 2:

Input: a = 4, b = 4, c = 6
Output: 7
Explanation: The starting state is (4, 4, 6). One optimal set of moves is:
– Take from 1st and 2nd piles, state is now (3, 3, 6)
– Take from 1st and 3rd piles, state is now (2, 3, 5)
– Take from 1st and 3rd piles, state is now (1, 3, 4)
– Take from 1st and 3rd piles, state is now (0, 3, 3)
– Take from 2nd and 3rd piles, state is now (0, 2, 2)
– Take from 2nd and 3rd piles, state is now (0, 1, 1)
– Take from 2nd and 3rd piles, state is now (0, 0, 0)
There are fewer than two non-empty piles, so the game ends. Total: 7 points.

Example 3:

Input: a = 1, b = 8, c = 8
Output: 8
Explanation: One optimal set of moves is to take from the 2nd and 3rd piles for 8 turns until they are empty.
After that, there are fewer than two non-empty piles, so the game ends.

Constraints:

1 <= a, b, c <= 105

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1753. Maximum Score From Removing Stones LeetCode Solution in C++

class Solution {
 public:
  int maximumScore(int a, int b, int c) {
    // the maximum <= the minimum + the middle
    const int x = (a + b + c) / 2;
    // the maximum > the minimum + the middle
    const int y = a + b + c - max({a, b, c});
    return min(x, y);
  }
};
/* code provided by PROGIEZ */

1753. Maximum Score From Removing Stones LeetCode Solution in Java

class Solution {
  public int maximumScore(int a, int b, int c) {
    // the maximum <= the minimum + the middle
    final int x = (a + b + c) / 2;
    // the maximum > the minimum + the middle
    final int y = a + b + c - Math.max(a, Math.max(b, c));
    return Math.min(x, y);
  }
}
// code provided by PROGIEZ

1753. Maximum Score From Removing Stones LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.