-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path740-DeleteAndEarn.py
More file actions
50 lines (46 loc) · 1.44 KB
/
740-DeleteAndEarn.py
File metadata and controls
50 lines (46 loc) · 1.44 KB
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
# Python3
class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
maxVal = max(nums)
total = [0] * (maxVal + 1)
for val in nums:
total[val] += val
def rob(nums: List[int]) -> int:
size = len(nums)
first, second = nums[0], max(nums[0], nums[1])
for i in range(2, size):
first, second = second, max(first + nums[i], second)
return second
return rob(total)
# C++
# 记元素 x 在数组中出现的次数为 c_x, 用一个数组 sum 记录数组 nums 中所有相同元素之和,
# 即 sum[x]=x⋅c_x, 若选择了 x,则可以获取 sum[x] 的点数,且无法再选择 x-1 和 x+1。
# 这与「198. 打家劫舍」是一样的。
class Solution {
private:
int rob(vector<int>& nums) {
int n = nums.size();
if (n == 1) {
return nums[0];
}
int first = nums[0], second = max(nums[0], nums[1]);
for (int i = 2; i < n; i++) {
int tmp = second;
second = max(first + nums[i], second);
first = tmp;
}
return second;
}
public:
int deleteAndEarn(vector<int>& nums) {
int maxVal = 0;
for (int val : nums) {
maxVal = max(maxVal, val);
}
vector<int> sum(maxVal + 1);
for (int val : nums) {
sum[val] += val;
}
return rob(sum);
}
};