Skip to content

Commit 4594404

Browse files
committed
feat: add solutions to leetcode problem: No.0494, No.740
1 parent b7d590b commit 4594404

File tree

8 files changed

+180
-9
lines changed

8 files changed

+180
-9
lines changed

solution/0400-0499/0494.Target Sum/README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,20 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
54+
if target < -1000 or target > 1000:
55+
return 0
56+
n = len(nums)
57+
dp = [[0] * 2001 for i in range(n)]
58+
dp[0][nums[0] + 1000] += 1
59+
dp[0][-nums[0] + 1000] += 1
60+
for i in range(1, n):
61+
for j in range(-1000, 1001):
62+
if dp[i - 1][j + 1000] > 0:
63+
dp[i][j + nums[i] + 1000] += dp[i - 1][j + 1000]
64+
dp[i][j - nums[i] + 1000] += dp[i - 1][j + 1000]
65+
return dp[n - 1][target + 1000]
5366
```
5467

5568
### **Java**
@@ -66,7 +79,7 @@ class Solution {
6679
int n = nums.length;
6780
int[][] dp = new int[n][2001];
6881

69-
dp[0][nums[0] + 1000] = 1;
82+
dp[0][nums[0] + 1000] += 1;
7083
dp[0][-nums[0] + 1000] += 1;
7184

7285
for (int i = 1; i < n; i++) {

solution/0400-0499/0494.Target Sum/README_EN.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,20 @@ It is similar to the backpack problem, except that the index may appear negative
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
60+
if target < -1000 or target > 1000:
61+
return 0
62+
n = len(nums)
63+
dp = [[0] * 2001 for i in range(n)]
64+
dp[0][nums[0] + 1000] += 1
65+
dp[0][-nums[0] + 1000] += 1
66+
for i in range(1, n):
67+
for j in range(-1000, 1001):
68+
if dp[i - 1][j + 1000] > 0:
69+
dp[i][j + nums[i] + 1000] += dp[i - 1][j + 1000]
70+
dp[i][j - nums[i] + 1000] += dp[i - 1][j + 1000]
71+
return dp[n - 1][target + 1000]
5972
```
6073

6174
### **Java**
@@ -70,7 +83,7 @@ class Solution {
7083
int n = nums.length;
7184
int[][] dp = new int[n][2001];
7285

73-
dp[0][nums[0] + 1000] = 1;
86+
dp[0][nums[0] + 1000] += 1;
7487
dp[0][-nums[0] + 1000] += 1;
7588

7689
for (int i = 1; i < n; i++) {

solution/0400-0499/0494.Target Sum/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public int findTargetSumWays(int[] nums, int target) {
77
int n = nums.length;
88
int[][] dp = new int[n][2001];
99

10-
dp[0][nums[0] + 1000] = 1;
10+
dp[0][nums[0] + 1000] += 1;
1111
dp[0][-nums[0] + 1000] += 1;
1212

1313
for (int i = 1; i < n; i++) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
3+
if target < -1000 or target > 1000:
4+
return 0
5+
n = len(nums)
6+
dp = [[0] * 2001 for i in range(n)]
7+
dp[0][nums[0] + 1000] += 1
8+
dp[0][-nums[0] + 1000] += 1
9+
for i in range(1, n):
10+
for j in range(-1000, 1001):
11+
if dp[i - 1][j + 1000] > 0:
12+
dp[i][j + nums[i] + 1000] += dp[i - 1][j + 1000]
13+
dp[i][j - nums[i] + 1000] += dp[i - 1][j + 1000]
14+
return dp[n - 1][target + 1000]

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

+48-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<pre>
1818
<strong>输入:</strong> nums = [3, 4, 2]
1919
<strong>输出:</strong> 6
20-
<strong>解释:</strong>
20+
<strong>解释:</strong>
2121
删除 4 来获得 4 个点数,因此 3 也被删除。
2222
之后,删除 2 来获得 2 个点数。总共获得 6 个点数。
2323
</pre>
@@ -27,7 +27,7 @@
2727
<pre>
2828
<strong>输入:</strong> nums = [2, 2, 3, 3, 3, 4]
2929
<strong>输出:</strong> 9
30-
<strong>解释:</strong>
30+
<strong>解释:</strong>
3131
删除 3 来获得 3 个点数,接着要删除两个 2 和 4 。
3232
之后,再次删除 3 获得 3 个点数,再次删除 3 获得 3 个点数。
3333
总共获得 9 个点数。
@@ -70,7 +70,21 @@ nonSelect[i] = Math.max(select[i-1], nonSelect[i-1]);
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```python
73-
73+
class Solution:
74+
def deleteAndEarn(self, nums: List[int]) -> int:
75+
mx = float('-inf')
76+
for num in nums:
77+
mx = max(mx, num)
78+
total = [0] * (mx + 1)
79+
for num in nums:
80+
total[num] += num
81+
first = total[0]
82+
second = max(total[0], total[1])
83+
for i in range(2, mx + 1):
84+
cur = max(first + total[i], second)
85+
first = second
86+
second = cur
87+
return second
7488
```
7589

7690
### **Java**
@@ -103,6 +117,37 @@ class Solution {
103117
}
104118
```
105119

120+
### **Go**
121+
122+
```go
123+
func deleteAndEarn(nums []int) int {
124+
125+
max := func(x, y int) int {
126+
if x > y {
127+
return x
128+
}
129+
return y
130+
}
131+
132+
mx := math.MinInt32
133+
for _, num := range nums {
134+
mx = max(mx, num)
135+
}
136+
total := make([]int, mx+1)
137+
for _, num := range nums {
138+
total[num] += num
139+
}
140+
first := total[0]
141+
second := max(total[0], total[1])
142+
for i := 2; i <= mx; i++ {
143+
cur := max(first+total[i], second)
144+
first = second
145+
second = cur
146+
}
147+
return second
148+
}
149+
```
150+
106151
### **...**
107152

108153
```

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

+46-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,21 @@ nonSelect[i] = Math.max(select[i-1], nonSelect[i-1]);
6565
### **Python3**
6666

6767
```python
68-
68+
class Solution:
69+
def deleteAndEarn(self, nums: List[int]) -> int:
70+
mx = float('-inf')
71+
for num in nums:
72+
mx = max(mx, num)
73+
total = [0] * (mx + 1)
74+
for num in nums:
75+
total[num] += num
76+
first = total[0]
77+
second = max(total[0], total[1])
78+
for i in range(2, mx + 1):
79+
cur = max(first + total[i], second)
80+
first = second
81+
second = cur
82+
return second
6983
```
7084

7185
### **Java**
@@ -96,6 +110,37 @@ class Solution {
96110
}
97111
```
98112

113+
### **Go**
114+
115+
```go
116+
func deleteAndEarn(nums []int) int {
117+
118+
max := func(x, y int) int {
119+
if x > y {
120+
return x
121+
}
122+
return y
123+
}
124+
125+
mx := math.MinInt32
126+
for _, num := range nums {
127+
mx = max(mx, num)
128+
}
129+
total := make([]int, mx+1)
130+
for _, num := range nums {
131+
total[num] += num
132+
}
133+
first := total[0]
134+
second := max(total[0], total[1])
135+
for i := 2; i <= mx; i++ {
136+
cur := max(first+total[i], second)
137+
first = second
138+
second = cur
139+
}
140+
return second
141+
}
142+
```
143+
99144
### **...**
100145

101146
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func deleteAndEarn(nums []int) int {
2+
3+
max := func(x, y int) int {
4+
if x > y {
5+
return x
6+
}
7+
return y
8+
}
9+
10+
mx := math.MinInt32
11+
for _, num := range nums {
12+
mx = max(mx, num)
13+
}
14+
total := make([]int, mx+1)
15+
for _, num := range nums {
16+
total[num] += num
17+
}
18+
first := total[0]
19+
second := max(total[0], total[1])
20+
for i := 2; i <= mx; i++ {
21+
cur := max(first+total[i], second)
22+
first = second
23+
second = cur
24+
}
25+
return second
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def deleteAndEarn(self, nums: List[int]) -> int:
3+
mx = float('-inf')
4+
for num in nums:
5+
mx = max(mx, num)
6+
total = [0] * (mx + 1)
7+
for num in nums:
8+
total[num] += num
9+
first = total[0]
10+
second = max(total[0], total[1])
11+
for i in range(2, mx + 1):
12+
cur = max(first + total[i], second)
13+
first = second
14+
second = cur
15+
return second

0 commit comments

Comments
 (0)