-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0740-delete-and-earn.cpp
68 lines (58 loc) · 2 KB
/
0740-delete-and-earn.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
vector<pair<int, int>> wn;
sort(nums.begin(), nums.end());
for (auto num: nums)
if (wn.empty() || wn.back().first != num) wn.push_back({num, 1});
else wn.back().second++;
sort(wn.begin(), wn.end(), [](auto l, auto r) {
return l.first * l.second > r.first * r.second;
});
for (auto [val, count]: wn) cout<<val<<" - "<<count<<endl;
int res = 0;
unordered_set<int> bag;
for (auto [val, count]: wn) {
if (bag.count(val + 1)) continue;
if (bag.count(val - 1)) continue;
bag.insert(val);
res += val * count;
}
return res;
}
};
*/
/*
There are two characteristics of this problem that hint towards the use of dynamic programming (DP). The first is that the problem is asking us to find the maximum of something. The second is that we need to make decisions on which numbers to take, and each decision may influence future decisions. For example, if we wanted to take all the fives, then we can no longer take the fours or sixes.
*/
// class Solution {
// public:
// int deleteAndEarn(vector<int>& nums) {
// unordered_map<int, int> um;
// for (auto num: nums) um[num]++;
// int take = 0, skip = 0;
// for (int i = 1; i < 10001; i++) {
// auto taking = skip + um[i] * i;
// skip = max(take, skip);
// take = taking;
// }
// return take;
// }
// };
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
int n = 10001;
vector<int> values(n, 0);
for (int num : nums)
values[num] += num;
int take = 0, skip = 0;
for (int i = 0; i < n; i++) {
int takei = skip + values[i];
skip = max(skip, take);
take = takei;
}
return max(take, skip);
}
};