Skip to content

Commit 57f4ad2

Browse files
authored
Create 1049-last-stone-weight-ii.py
1 parent c6de535 commit 57f4ad2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

python/1049-last-stone-weight-ii.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def lastStoneWeightII(self, stones: List[int]) -> int:
3+
# Memoization
4+
stoneSum = sum(stones)
5+
target = ceil(stoneSum / 2)
6+
7+
def dfs(i, total):
8+
if total >= target or i == len(stones):
9+
return abs(total - (stoneSum - total))
10+
if (i, total) in dp:
11+
return dp[(i, total)]
12+
13+
dp[(i, total)] = min(dfs(i + 1, total),
14+
dfs(i + 1, total + stones[i]))
15+
return dp[(i, total)]
16+
17+
dp = {}
18+
return dfs(0, 0)

0 commit comments

Comments
 (0)