Skip to content

Commit 49c8750

Browse files
authored
Update Last Stone Weight - Leetcode 1046.py
1 parent a932317 commit 49c8750

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Last Stone Weight - Leetcode 1046/Last Stone Weight - Leetcode 1046.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
# Brute Force Solution
2+
class Solution:
3+
def lastStoneWeight(self, stones: List[int]) -> int:
4+
5+
def remove_largest():
6+
index_of_largest = stones.index(max(stones))
7+
return stones.pop(index_of_largest)
8+
9+
while len(stones) > 1:
10+
stone_1 = remove_largest()
11+
stone_2 = remove_largest()
12+
if stone_1 != stone_2:
13+
stones.append(stone_1 - stone_2)
14+
15+
return stones[0] if stones else 0
16+
# Time: O(n^2)
17+
# Space: O(1)
18+
19+
# Optimal Solution
120
class Solution:
221
def lastStoneWeight(self, stones: List[int]) -> int:
322
for i in range(len(stones)):

0 commit comments

Comments
 (0)