Maximum Score From Removing Stones — LeetCode 1753 Python Solution

MediumGreedyMathHeap (Priority Queue)
Problem
#1753
Reading time
2 min

The problem

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.

Example

Input
a = 2, b = 4, c = 6
Output
6
Explanation
The starting state is (2, 4, 6). One optimal set of moves is:

Python solution

Python
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

Complexity

MeasureComplexity
TimeO(n log n)
SpaceO(1) to O(n) auxiliary

Pattern: Heap / Priority Queue

Keep only the best k elements, or always pull the smallest, in log time. LeetCode 1753. Maximum Score From Removing Stones is filed here because LeetCode tags it Heap (Priority Queue), which is the vocabulary this hub collects.

The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1753. Maximum Score From Removing Stones?
LeetCode 1753. Maximum Score From Removing Stones is rated Medium on LeetCode.
What topics does LeetCode 1753. Maximum Score From Removing Stones cover?
LeetCode 1753. Maximum Score From Removing Stones is tagged Greedy, Math and Heap (Priority Queue) on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview