Skip to content

Commit 635dcfd

Browse files
committed
0740 补充英文解和Solution.java
1 parent 331870a commit 635dcfd

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

solution/0700-0799/0740.Delete and Earn/README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@
4646
<!-- tabs:start -->
4747
核心思路: **一个数字要么不选,要么全选**
4848

49-
首先计算出每个数字的总和sums,并维护两个dp数组:select和nonSelect
49+
首先计算出每个数字的总和 sums,并维护两个 dp 数组:select 和 nonSelect
5050

51-
- sums[i]代表值为i的元素总和
52-
- select[i]代表如果选数字i,从0处理到i的最大和
53-
- nonSelect[i]代表如果不选数字i,从0处理到i的最大和
51+
- sums[i] 代表值为 i 的元素总和
52+
- select[i] 代表如果选数字 i,从 0 处理到 i 的最大和
53+
- nonSelect[i] 代表如果不选数字 i,从 0 处理到 i 的最大和
5454

5555
那么我们有以下逻辑:
5656

57-
- 如果选i,那么i-1肯定不能选
58-
- 如果不选i,那么i-1选不选都可以,因此我们选择其中较大的选法
57+
- 如果选 i,那么 i-1 肯定不能选
58+
- 如果不选 i,那么 i-1 选不选都可以,因此我们选择其中较大的选法
5959

6060
``` java
6161
select[i] = nonSelect[i-1] + sums[i];
@@ -78,24 +78,27 @@ class Solution {
7878
if (nums.length == 0) {
7979
return 0;
8080
}
81+
8182
int[] sums = new int[10010];
83+
int[] select = new int[10010];
84+
int[] nonSelect = new int[10010];
85+
86+
int maxV = 0;
8287
for (int x : nums) {
8388
sums[x] += x;
89+
maxV = Math.max(maxV, x);
8490
}
85-
int[] select = new int[10010];
86-
int[] nonSelect = new int[10010];
87-
for (int i = 1; i <= 10000; i++) {
91+
92+
for (int i = 1; i <= maxV; i++) {
8893
select[i] = nonSelect[i - 1] + sums[i];
8994
nonSelect[i] = Math.max(select[i - 1], nonSelect[i - 1]);
9095
}
91-
92-
return Math.max(select[10000], nonSelect[10000]);
96+
return Math.max(select[maxV], nonSelect[maxV]);
9397
}
9498
}
9599
```
96100

97101
### **...**
98-
99102
```
100103
101104
```

solution/0700-0799/0740.Delete and Earn/README_EN.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,24 @@ Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
8686

8787
## Solutions
8888

89-
9089
<!-- tabs:start -->
90+
Intuition: **If we take a number, we will take all of the copies of it**.
91+
92+
First calculate the sum of each number as **sums**, and keep updating two dp arrays: **select** and **nonSelect**
93+
94+
- sums[i] represents the sum of elements whose value is i;
95+
- select[i] represents the maximum sum of processing from 0 to i if the number i is selected;
96+
- nonSelect[i] represents the maximum sum of processing from 0 to i if the number i is not selected;
97+
98+
Then we have the following conclusions:
99+
100+
- If i is selected, then i-1 must not be selected;
101+
- If you do not choose i, then i-1 can choose or not, so we choose the larger one;
102+
103+
```java
104+
select[i] = nonSelect[i-1] + sums[i];
105+
nonSelect[i] = Math.max(select[i-1], nonSelect[i-1]);
106+
```
91107

92108
### **Python3**
93109

@@ -98,7 +114,29 @@ Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
98114
### **Java**
99115

100116
```java
101-
117+
class Solution {
118+
public int deleteAndEarn(int[] nums) {
119+
if (nums.length == 0) {
120+
return 0;
121+
}
122+
123+
int[] sums = new int[10010];
124+
int[] select = new int[10010];
125+
int[] nonSelect = new int[10010];
126+
127+
int maxV = 0;
128+
for (int x : nums) {
129+
sums[x] += x;
130+
maxV = Math.max(maxV, x);
131+
}
132+
133+
for (int i = 1; i <= maxV; i++) {
134+
select[i] = nonSelect[i - 1] + sums[i];
135+
nonSelect[i] = Math.max(select[i - 1], nonSelect[i - 1]);
136+
}
137+
return Math.max(select[maxV], nonSelect[maxV]);
138+
}
139+
}
102140
```
103141

104142
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public int deleteAndEarn(int[] nums) {
3+
if (nums.length == 0) {
4+
return 0;
5+
}
6+
7+
int[] sums = new int[10010];
8+
int[] select = new int[10010];
9+
int[] nonSelect = new int[10010];
10+
11+
int maxV = 0;
12+
for (int x : nums) {
13+
sums[x] += x;
14+
maxV = Math.max(maxV, x);
15+
}
16+
17+
for (int i = 1; i <= maxV; i++) {
18+
select[i] = nonSelect[i - 1] + sums[i];
19+
nonSelect[i] = Math.max(select[i - 1], nonSelect[i - 1]);
20+
}
21+
return Math.max(select[maxV], nonSelect[maxV]);
22+
}
23+
}

0 commit comments

Comments
 (0)