Skip to content

Commit d1cb147

Browse files
committed
feat: add last stone weight solution
1 parent 38a28ab commit d1cb147

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

alternative/easy/last_stone_weight.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
import heapq
3+
4+
def lastStoneWeight(stones: List[int]) -> int:
5+
maxHeap = []
6+
for stone in stones:
7+
heapq.heappush(maxHeap, -stone)
8+
while len(maxHeap) > 1:
9+
stone1 = -heapq.heappop(maxHeap)
10+
stone2 = -heapq.heappop(maxHeap)
11+
if stone1 != stone2:
12+
heapq.heappush(maxHeap, -(stone1 - stone2))
13+
return -maxHeap[0] if maxHeap else 0
14+
15+
assert lastStoneWeight([2,7,4,1,8,1]) == 1

0 commit comments

Comments
 (0)