We use a Max-Heap to efficiently retrieve the two heaviest stones in each step.
- Convert all stones to negative values and
heapifythem to create a Max-Heap. - While there is more than 1 stone:
- Pop two stones
s1ands2. - If
s1 != s2, calculate the differences1 - s2(orabs(s1-s2)) and push it back.
- Pop two stones
- If one stone remains, return its absolute value; otherwise, return 0.
- Time Complexity: O(N log N).
- Space Complexity: O(N).
import heapq
def last_stone_weight(stones):
# Python uses min-heap, so negate values for max-heap
h = [-s for s in stones]
heapq.heapify(h)
while len(h) > 1:
s1 = heapq.heappop(h)
s2 = heapq.heappop(h)
# If they are different, push back the positive difference
if s1 != s2:
heapq.heappush(h, s1 - s2)
return -h[0] if h else 0