Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
2 4 6
4 4 6
1 8 8
minimum-amount-of-time-to-fill-cups)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1753: Maximum Score From Removing Stones
class Solution {
public int maximumScore(int a, int b, int c) {
int[] s = new int[] {a, b, c};
Arrays.sort(s);
int ans = 0;
while (s[1] > 0) {
++ans;
s[1]--;
s[2]--;
Arrays.sort(s);
}
return ans;
}
}
// Accepted solution for LeetCode #1753: Maximum Score From Removing Stones
func maximumScore(a int, b int, c int) (ans int) {
s := []int{a, b, c}
sort.Ints(s)
for s[1] > 0 {
ans++
s[1]--
s[2]--
sort.Ints(s)
}
return
}
# Accepted solution for LeetCode #1753: Maximum Score From Removing Stones
class Solution:
def maximumScore(self, a: int, b: int, c: int) -> int:
s = sorted([a, b, c])
ans = 0
while s[1]:
ans += 1
s[1] -= 1
s[2] -= 1
s.sort()
return ans
// Accepted solution for LeetCode #1753: Maximum Score From Removing Stones
struct Solution;
impl Solution {
fn maximum_score(a: i32, b: i32, c: i32) -> i32 {
let sum = a + b + c;
let min = a.max(b).max(c);
(sum / 2).min(sum - min)
}
}
#[test]
fn test() {
let a = 2;
let b = 4;
let c = 6;
let res = 6;
assert_eq!(Solution::maximum_score(a, b, c), res);
let a = 4;
let b = 4;
let c = 6;
let res = 7;
assert_eq!(Solution::maximum_score(a, b, c), res);
let a = 1;
let b = 8;
let c = 8;
let res = 8;
assert_eq!(Solution::maximum_score(a, b, c), res);
}
// Accepted solution for LeetCode #1753: Maximum Score From Removing Stones
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1753: Maximum Score From Removing Stones
// class Solution {
// public int maximumScore(int a, int b, int c) {
// int[] s = new int[] {a, b, c};
// Arrays.sort(s);
// int ans = 0;
// while (s[1] > 0) {
// ++ans;
// s[1]--;
// s[2]--;
// Arrays.sort(s);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.