Skip to content

Commit 3a4f89d

Browse files
authored
Create Last Stone Weight - Leetcode 1046.py
1 parent b98fbd2 commit 3a4f89d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Last Stone Weight - Leetcode 1046.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def lastStoneWeight(self, stones: List[int]) -> int:
3+
for i in range(len(stones)):
4+
stones[i] *= -1 # Negate to force Max Heap
5+
6+
heapq.heapify(stones)
7+
8+
while len(stones) > 1:
9+
largest = heapq.heappop(stones)
10+
next_largest = heapq.heappop(stones)
11+
12+
if largest != next_largest:
13+
heapq.heappush(stones, largest - next_largest)
14+
15+
return -heapq.heappop(stones) if stones else 0
16+
# Time: O(n log n)
17+
# Space: O(1)
18+

0 commit comments

Comments
 (0)