@@ -42,15 +42,13 @@ Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
42
42
43
43
## Solutions
44
44
45
- <!-- tabs:start -->
46
-
47
45
Intuition: ** If we take a number, we will take all of the copies of it** .
48
46
49
47
First calculate the sum of each number as ** sums** , and keep updating two dp arrays: ** select** and ** nonSelect**
50
48
51
- - sums[ i] represents the sum of elements whose value is i;
52
- - select[ i] represents the maximum sum of processing from 0 to i if the number i is selected;
53
- - nonSelect[ i] represents the maximum sum of processing from 0 to i if the number i is not selected;
49
+ - ` sums[i] ` represents the sum of elements whose value is i;
50
+ - ` select[i] ` represents the maximum sum of processing from 0 to i if the number i is selected;
51
+ - ` nonSelect[i] ` represents the maximum sum of processing from 0 to i if the number i is not selected;
54
52
55
53
Then we have the following conclusions:
56
54
@@ -62,6 +60,8 @@ select[i] = nonSelect[i-1] + sums[i];
62
60
nonSelect[i] = Math . max(select[i- 1 ], nonSelect[i- 1 ]);
63
61
```
64
62
63
+ <!-- tabs:start -->
64
+
65
65
### ** Python3**
66
66
67
67
``` python
@@ -141,6 +141,31 @@ func deleteAndEarn(nums []int) int {
141
141
}
142
142
```
143
143
144
+ ### ** C++**
145
+
146
+ ``` cpp
147
+ class Solution {
148
+ public:
149
+ int deleteAndEarn(vector<int >& nums) {
150
+ vector<int > vals(10010);
151
+ for (int& num : nums) {
152
+ vals[ num] += num;
153
+ }
154
+ return rob(vals);
155
+ }
156
+
157
+ int rob(vector<int>& nums) {
158
+ int a = 0, b = nums[0];
159
+ for (int i = 1; i < nums.size(); ++i) {
160
+ int c = max(nums[i] + a, b);
161
+ a = b;
162
+ b = c;
163
+ }
164
+ return b;
165
+ }
166
+ };
167
+ ```
168
+
144
169
### ** ...**
145
170
146
171
```
0 commit comments