Skip to content

Commit 996c97a

Browse files
committed
python: Create delete and earn.
1 parent 7d8d654 commit 996c97a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

python/0740-delete-and-earn.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# House Robber Style
2+
# Time Complexity O(n)
3+
# Space Complexity O(n)
4+
class Solution(object):
5+
def deleteAndEarn(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
11+
upperLimit = max(nums) + 1
12+
store = [0] * upperLimit
13+
14+
for num in nums:
15+
store[num] += num
16+
17+
dp = [0] * upperLimit
18+
19+
dp[1] = 1 * store[1]
20+
for i in range(2, upperLimit):
21+
dp[i] = max(dp[i - 2] + store[i], dp[i - 1])
22+
23+
return dp[-1]

0 commit comments

Comments
 (0)