InferenceAI

Enter at least two characters to search.

Start free

Smash the Stones

easyHeaps15 min10 XP

Each turn, take the two heaviest stones with weights x≤yx \le y. If x=yx = y both are destroyed; otherwise a stone of weight y−xy - x remains. Return the weight of the last stone, or 0 if none remain.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[2,7,4,1,8,1]]
TASKsmash_the_stones()
EXPECTED OUTPUT1

Start with the input values shown in this example.

Example 1 of 3

Input: stones = [2,7,4,1,8,1]

Output: 1

Explanation: classic

Input: stones = [1]

Output: 1

Explanation: single

Input: stones = [3,3]

Output: 0

Explanation: both destroyed

Hint 1

Use a max-heap (in Python, push negated weights onto heapq).

Topics

DSAHeapsPython