Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e816450

Browse files
committed
feat: update solutions to lc problem: No.0740. Delete and Earn
1 parent bcd187d commit e816450

File tree

6 files changed

+89
-25
lines changed

6 files changed

+89
-25
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
- [使用最小花费爬楼梯](./solution/0700-0799/0746.Min%20Cost%20Climbing%20Stairs/README.md)
165165
- [打家劫舍](./solution/0100-0199/0198.House%20Robber/README.md)
166166
- [打家劫舍 II](./solution/0200-0299/0213.House%20Robber%20II/README.md)
167+
- [删除并获得点数](./solution/0700-0799/0740.Delete%20and%20Earn/README.md)
167168
- [接雨水](./solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
168169
- [最大子序和](./solution/0000-0099/0053.Maximum%20Subarray/README.md)
169170
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)

‎README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
158158
- [Min Cost Climbing Stairs](./solution/0700-0799/0746.Min%20Cost%20Climbing%20Stairs/README_EN.md)
159159
- [House Robber](./solution/0100-0199/0198.House%20Robber/README_EN.md)
160160
- [House Robber II](./solution/0200-0299/0213.House%20Robber%20II/README_EN.md)
161+
- [Delete and Earn](./solution/0700-0799/0740.Delete%20and%20Earn/README_EN.md)
161162
- [Trapping Rain Water](./solution/0000-0099/0042.Trapping%20Rain%20Water/README_EN.md)
162163
- [Maximum Subarray](./solution/0000-0099/0053.Maximum%20Subarray/README_EN.md)
163164
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)

‎solution/0100-0199/0198.House Robber/README.md‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,14 @@ public:
8686
class Solution {
8787
public:
8888
int rob(vector<int>& nums) {
89-
int n;
90-
if ((n = nums.size()) == 0) return 0;
91-
return robRange(nums, 0, n - 1);
92-
}
93-
94-
private:
95-
int robRange(vector<int>& nums, int start, int end) {
96-
if (end - start == 0) return nums[start];
97-
int pre = 0;
98-
int cur = nums[start];
99-
for (int i = start + 1; i < end + 1; ++i) {
100-
int t = max(pre + nums[i], cur);
101-
pre = cur;
102-
cur = t;
89+
int n = nums.size();
90+
int a = 0, b = nums[0];
91+
for (int i = 1; i < n; ++i) {
92+
int c = max(nums[i] + a, b);
93+
a = b;
94+
b = c;
10395
}
104-
return cur;
96+
return b;
10597
}
10698
};
10799
```

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
<!-- tabs:start -->
49-
5048
核心思路: **一个数字要么不选,要么全选**
5149

5250
首先计算出每个数字的总和 sums,并维护两个 dp 数组:select 和 nonSelect
5351

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

5856
那么我们有以下逻辑:
5957

@@ -65,6 +63,8 @@ select[i] = nonSelect[i-1] + sums[i];
6563
nonSelect[i] = Math.max(select[i-1], nonSelect[i-1]);
6664
```
6765

66+
<!-- tabs:start -->
67+
6868
### **Python3**
6969

7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -148,6 +148,31 @@ func deleteAndEarn(nums []int) int {
148148
}
149149
```
150150

151+
### **C++**
152+
153+
```cpp
154+
class Solution {
155+
public:
156+
int deleteAndEarn(vector<int>& nums) {
157+
vector<int> vals(10010);
158+
for (int& num : nums) {
159+
vals[num] += num;
160+
}
161+
return rob(vals);
162+
}
163+
164+
int rob(vector<int>& nums) {
165+
int a = 0, b = nums[0];
166+
for (int i = 1; i < nums.size(); ++i) {
167+
int c = max(nums[i] + a, b);
168+
a = b;
169+
b = c;
170+
}
171+
return b;
172+
}
173+
};
174+
```
175+
151176
### **...**
152177

153178
```

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
4242

4343
## Solutions
4444

45-
<!-- tabs:start -->
46-
4745
Intuition: **If we take a number, we will take all of the copies of it**.
4846

4947
First calculate the sum of each number as **sums**, and keep updating two dp arrays: **select** and **nonSelect**
5048

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;
5452

5553
Then we have the following conclusions:
5654

@@ -62,6 +60,8 @@ select[i] = nonSelect[i-1] + sums[i];
6260
nonSelect[i] = Math.max(select[i-1], nonSelect[i-1]);
6361
```
6462

63+
<!-- tabs:start -->
64+
6565
### **Python3**
6666

6767
```python
@@ -141,6 +141,31 @@ func deleteAndEarn(nums []int) int {
141141
}
142142
```
143143

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+
144169
### **...**
145170

146171
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int deleteAndEarn(vector<int>& nums) {
4+
vector<int> vals(10010);
5+
for (int& num : nums) {
6+
vals[num] += num;
7+
}
8+
return rob(vals);
9+
}
10+
11+
int rob(vector<int>& nums) {
12+
int a = 0, b = nums[0];
13+
for (int i = 1; i < nums.size(); ++i) {
14+
int c = max(nums[i] + a, b);
15+
a = b;
16+
b = c;
17+
}
18+
return b;
19+
}
20+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /