Skip to content

Commit 67e57f4

Browse files
Merge pull request #2975 from coopers/1046
Update 1046-last-stone-weight.py
2 parents bbf7f56 + 17c1505 commit 67e57f4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

python/1046-last-stone-weight.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,19 @@ def lastStoneWeight(self, stones: List[int]) -> int:
1111

1212
stones.append(0)
1313
return abs(stones[0])
14+
15+
# There's a private _heapify_max method.
16+
# https://github.com/python/cpython/blob/1170d5a292b46f754cd29c245a040f1602f70301/Lib/heapq.py#L198
17+
class Solution(object):
18+
def lastStoneWeight(self, stones):
19+
heapq._heapify_max(stones)
20+
while len(stones) > 1:
21+
max_stone = heapq._heappop_max(stones)
22+
diff = max_stone - stones[0]
23+
if diff:
24+
heapq._heapreplace_max(stones, diff)
25+
else:
26+
heapq._heappop_max(stones)
27+
28+
stones.append(0)
29+
return stones[0]

0 commit comments

Comments
 (0)