Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find maximum score from removing stones in Python
Suppose we have three values a, b and c representing three piles of stones. We are playing a solitaire game where each turn the player selects two different non-empty piles, takes one stone from each, and adds 1 point to their score. The game ends when there are fewer than two non-empty piles. We need to find the maximum score possible.
So, if the input is like a = 4, b = 4, c = 6, then the output will be 7 because the initial state is (4, 4, 6), then we can follow these steps −
Select from 1st and 2nd piles so current state is (3, 3, 6)
Select from 1st and 3rd piles so current state is (2, 3, 5)
Select from 1st and 3rd piles so current state is (1, 3, 4)
Select from 1st and 3rd piles so current state is (0, 3, 3)
Select from 2nd and 3rd piles so current state is (0, 2, 2)
Select from 2nd and 3rd piles so current state is (0, 1, 1)
Select from 2nd and 3rd piles so current state is (0, 0, 0)
And finally there are fewer than two non-empty piles, so the game ends with a score of 7.
Algorithm
To solve this problem efficiently, we follow these steps −
minimum := minimum of a, b and c
maximum := maximum of a, b and c
left := a + b + c - maximum - minimum (the middle value)
-
if maximum - left <= minimum, then
return minimum + left - quotient of (1 + minimum - (maximum - left))/2
otherwise, return minimum + min(maximum - minimum, left)
Example
Let us see the following implementation to get better understanding −
def solve(a, b, c):
minimum = min(a, b, c)
maximum = max(a, b, c)
left = a + b + c - maximum - minimum
if maximum - left <= minimum:
return minimum + left - (1 + minimum - (maximum - left)) // 2
return minimum + min(maximum - minimum, left)
# Test with the given example
a = 4
b = 4
c = 6
result = solve(a, b, c)
print(f"Maximum score for piles ({a}, {b}, {c}): {result}")
Maximum score for piles (4, 4, 6): 7
How It Works
The key insight is that we can always take stones from any two non-empty piles. The optimal strategy depends on the relative sizes of the three piles:
If one pile is much larger than the others, we should prioritize reducing it by pairing it with smaller piles
The maximum possible score is limited by either the sum of the two smaller piles or by balancing constraints when piles are more equal in size
Testing with Different Examples
def solve(a, b, c):
minimum = min(a, b, c)
maximum = max(a, b, c)
left = a + b + c - maximum - minimum
if maximum - left <= minimum:
return minimum + left - (1 + minimum - (maximum - left)) // 2
return minimum + min(maximum - minimum, left)
# Test cases
test_cases = [(4, 4, 6), (2, 4, 6), (10, 10, 10), (1, 1, 8)]
for a, b, c in test_cases:
result = solve(a, b, c)
print(f"Piles ({a}, {b}, {c}) ? Maximum score: {result}")
Piles (4, 4, 6) ? Maximum score: 7 Piles (2, 4, 6) ? Maximum score: 6 Piles (10, 10, 10) ? Maximum score: 15 Piles (1, 1, 8) ? Maximum score: 2
Conclusion
The maximum score in this stone removal game can be calculated using a mathematical approach that considers the relative sizes of the three piles. The algorithm runs in O(1) time complexity, making it very efficient compared to simulating all possible game moves.
