-
Notifications
You must be signed in to change notification settings - Fork 43
/
delete-and-earn.py
54 lines (50 loc) · 1.41 KB
/
delete-and-earn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# V0
# V1
# http://bookshadow.com/weblog/2017/12/03/leetcode-delete-and-earn/
import collections
class Solution(object):
def deleteAndEarn(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt = collections.Counter(nums)
maxn = max(nums + [0])
dp = [0] * (maxn + 10)
for x in range(1, maxn + 1):
dp[x] = max(dp[x - 1], dp[x - 2] + cnt[x] * x)
return dp[maxn]
# V1'
# https://www.jiuzhang.com/solution/delete-and-earn/#tag-highlight-lang-python
class Solution:
"""
@param nums: a list of integers
@return: return a integer
"""
def deleteAndEarn(self, nums):
# write your code here
counters = [0 for i in range(10001)]
for x in nums:
counters[x] += 1
dp = [0 for i in range(10001)]
dp[1] = counters[1]
for i in range(2, 10001):
dp[i] = max(dp[i - 1], dp[i - 2] + i * counters[i])
return dp[10000]
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def deleteAndEarn(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
vals = [0] * 10001
for num in nums:
vals[num] += num
val_i, val_i_1 = vals[0], 0
for i in range(1, len(vals)):
val_i_1, val_i_2 = val_i, val_i_1
val_i = max(vals[i] + val_i_2, val_i_1)
return val_i