Skip to content

Commit 6a58f99

Browse files
committed
docs: add last stone weight description algorithm
1 parent d1cb147 commit 6a58f99

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

alternative/easy/last_stone_weight.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@ def lastStoneWeight(stones: List[int]) -> int:
1212
heapq.heappush(maxHeap, -(stone1 - stone2))
1313
return -maxHeap[0] if maxHeap else 0
1414

15-
assert lastStoneWeight([2,7,4,1,8,1]) == 1
15+
assert lastStoneWeight([2,7,4,1,8,1]) == 1
16+
17+
# Algorithm - Max Heap
18+
# Time Complexity - O(nlogn)
19+
# Space Complexity - O(n)
20+
21+
# Description
22+
23+
# 1. Initialize a maxHeap
24+
# 2. For each stone in stones, push the negative stone to the maxHeap
25+
# 3. While the length of the maxHeap is greater than 1:
26+
# 4. Pop the maxHeap and assign it to stone1
27+
# 5. Pop the maxHeap and assign it to stone2
28+
# 6. If stone1 is not equal to stone2, push the difference of stone1 and stone2 to the maxHeap
29+
# 7. Return the negative of the maxHeap[0] if the maxHeap is not empty, otherwise return 0

0 commit comments

Comments
 (0)