Last Stone Weight
Problem Link: https://leetcode.com/problems/last-stone-weight/
import heapq
class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
maxheap=[-stone for stone in stones]
heapq.heapify(maxheap)
# print(maxheap)
# Ex: [2,7,4,1,8,1]
while(len(maxheap)>=2):
a=heapq.heappop(maxheap)
b=heapq.heappop(maxheap)
# a=-8 and b=-7
# We need to push -1
if(a<b):
heapq.heappush(maxheap,a-b)
if(len(maxheap)==0):
return 0
else:
return abs(maxheap[0])
Last updated