Problem Description
Given three piles of stones with counts a, b, and c, you can remove one stone from any two distinct non-empty piles per move, earning 1 point per move. The game terminates when fewer than two piles have stones. The goal is to compute the maximum achievable score.
Key Insights
- Every move removes exactly two stones, so without restrictions, the total possible moves would be (a + b + c) / 2.
- The game is limited by the situation where one pile has significantly more stones than the sum of the other two; moves can only be made until the two smaller piles are exhausted.
- Therefore, the maximum score is the minimum of the total stones divided by 2, and the sum of the two smallest piles (or, equivalently, the total stones minus the largest pile).
Space and Time Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Solution
The key observation is that each move uses two stones, so the maximum possible number of moves is (a + b + c) / 2 if all stones could be paired perfectly. However, if one pile is too large (i.e., larger than the sum of the other two piles), then the extra stones in that pile cannot form a pair once the other two are exhausted. Thus, the answer is the smaller value between (a + b + c) / 2 and (a + b + c) minus the largest pile. This solution uses basic arithmetic operations and is achieved in constant time and space.