Skip to content

Commit d0bdeff

Browse files
authored
Merge pull request doocs#298 from gengyuanzhe/master
0740 Delete and Earn新增Java解
2 parents 7838230 + 635dcfd commit d0bdeff

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

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

+40-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,24 @@
4343
## 解法
4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
4746
<!-- tabs:start -->
47+
核心思路: **一个数字要么不选,要么全选**
48+
49+
首先计算出每个数字的总和 sums,并维护两个 dp 数组:select 和 nonSelect
50+
51+
- sums[i] 代表值为 i 的元素总和
52+
- select[i] 代表如果选数字 i,从 0 处理到 i 的最大和
53+
- nonSelect[i] 代表如果不选数字 i,从 0 处理到 i 的最大和
54+
55+
那么我们有以下逻辑:
56+
57+
- 如果选 i,那么 i-1 肯定不能选;
58+
- 如果不选 i,那么 i-1 选不选都可以,因此我们选择其中较大的选法
59+
60+
``` java
61+
select[i] = nonSelect[i-1] + sums[i];
62+
nonSelect[i] = Math.max(select[i-1], nonSelect[i-1]);
63+
```
4864

4965
### **Python3**
5066
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -57,7 +73,29 @@
5773
<!-- 这里可写当前语言的特殊实现逻辑 -->
5874

5975
```java
60-
76+
class Solution {
77+
public int deleteAndEarn(int[] nums) {
78+
if (nums.length == 0) {
79+
return 0;
80+
}
81+
82+
int[] sums = new int[10010];
83+
int[] select = new int[10010];
84+
int[] nonSelect = new int[10010];
85+
86+
int maxV = 0;
87+
for (int x : nums) {
88+
sums[x] += x;
89+
maxV = Math.max(maxV, x);
90+
}
91+
92+
for (int i = 1; i <= maxV; i++) {
93+
select[i] = nonSelect[i - 1] + sums[i];
94+
nonSelect[i] = Math.max(select[i - 1], nonSelect[i - 1]);
95+
}
96+
return Math.max(select[maxV], nonSelect[maxV]);
97+
}
98+
}
6199
```
62100

63101
### **...**

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

+40-2
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
### **...**
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)