#1753
Medium Algorithms Maximum score from removing stones
Math Greedy Heap (Priority Queue)
68.6% acceptance
Feb 25, 2026
986
58
There are three piles of stones of sizes a, b, and c respectively.
Each turn you choose two of the piles, remove one stone from each of those two piles.
The game stops when any two of the three piles are empty.
The score is the total number of removed stones.
Return the maximum score you can get.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn maximum_score(a: i32, b: i32, c: i32) -> i32 {
let total = a + b + c;
let max_val = a.max(b).max(c);
// If the largest pile is bigger than the sum of the other two, we can only use (sum of other two) pairs
// Otherwise, we can use total / 2 pairs
(total - max_val).min(total / 2)
}
}