Skip to content

Commit 1dc4ca7

Browse files
committed
update 125
1 parent 65db7b8 commit 1dc4ca7

File tree

5 files changed

+187
-41
lines changed

5 files changed

+187
-41
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ My personal leetcode answers<br/>
66
This is a **continually updated** open source project<br/>
77
<br/>
88
Total sovled: **339**<br/>
9-
Auto updated at: **2024-07-15 10:44:48**<br/>
9+
Auto updated at: **2024-07-15 10:53:35**<br/>
1010

1111
## 软件/Softwares
1212
- [Anki](https://apps.ankiweb.net/)
@@ -193,7 +193,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
193193
| [Leetcode-3048. Earliest Second To Mark Indices I](https://leetcode.com/problems/earliest-second-to-mark-indices-i/description/) | [python](./leetcode/3048.earliest-second-to-mark-indices-i.py), [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp) | O\(M\*logM\) | O\(M\) | \- | - |
194194

195195
## Dynamic Programming
196-
| Problem(18) | Solution | Time | Space | Note | Ref |
196+
| Problem(19) | Solution | Time | Space | Note | Ref |
197197
| ----- | ----- | ----- | ----- | ----- | ----- |
198198
| [Leetcode-64. Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/description/) | [c++](./leetcode/64.minimum-path-sum.cpp), [python](./leetcode/64.minimum-path-sum.py) | O\(MN\) | O\(MN\) | \- | - |
199199
| [Leetcode-70. Climbing Stairs](https://leetcode.com/problems/climbing-stairs/description/) | [c++](./leetcode/70.climbing-stairs.cpp), [python](./leetcode/70.climbing-stairs.py) | O\(N\) | O\(1\) | \- | - |
@@ -211,7 +211,8 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
211211
| [Leetcode-435. Non Overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/description/) | [c++](./leetcode/435.non-overlapping-intervals.cpp), [python](./leetcode/435.non-overlapping-intervals.py) | O\(N \* logN\) | O\(1\) | \- | - |
212212
| [Leetcode-542. 01 Matrix](https://leetcode.com/problems/01-matrix/description/) | [python](./leetcode/542.01-matrix.py), [c++](./leetcode/542.01-matrix.cpp) | O\(MN\) | O\(MN\) | \- | - |
213213
| [Leetcode-1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/description/) | [python](./leetcode/1143.longest-common-subsequence.py), [c++](./leetcode/1143.longest-common-subsequence.cpp) | O\(MN\) | O\(MN\) | \- | - |
214-
| [Lintcode-92. Backpack](https://www.lintcode.com/problem/backpack) | [python](./lintcode/92.backpack.py), [c++](./lintcode/92.backpack.cpp) | \- | \- | \- | - |
214+
| [Lintcode-92. Backpack](https://www.lintcode.com/problem/backpack) | [python](./lintcode/92.backpack.py), [c++](./lintcode/92.backpack.cpp) | O\(N^2\) | O\(N^2\) | \- | - |
215+
| [Lintcode-125. Backpack II](https://www.lintcode.com/problem/backpack-ii) | [python](./lintcode/125.backpack-ii.py), [c++](./lintcode/125.backpack-ii.cpp) | O\(N^2\) | O\(N^2\) | \- | - |
215216
| [Lintcode-437. Copy Books](https://www.lintcode.com/problem/copy-books) | [c++](./lintcode/437.copy-books.cpp), [python](./lintcode/437.copy-books.py) | O\(N\*logP\) | O\(1\) | Answer \| DP | - |
216217

217218
## Backtracking
@@ -317,7 +318,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
317318
| [Lintcode-1790. Rotate String II](https://www.lintcode.com/problem/rotate-string-ii) | [c++](./lintcode/1790.rotate-string-ii.cpp), [python](./lintcode/1790.rotate-string-ii.py) | O\(N\) | O\(N\) | Simulation | - |
318319

319320
## Other
320-
| Problem(244) | Solution | Time | Space | Note | Ref |
321+
| Problem(243) | Solution | Time | Space | Note | Ref |
321322
| ----- | ----- | ----- | ----- | ----- | ----- |
322323
| [Leetcode-1. Two Sum](https://leetcode.com/problems/two-sum/description/) | [python](./leetcode/1.two-sum.py), [c++](./leetcode/1.two-sum.cpp) | \- | \- | \- | - |
323324
| [Leetcode-2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) | [python](./leetcode/2.add-two-numbers.py), [c++](./leetcode/2.add-two-numbers.cpp) | \- | \- | \- | - |
@@ -539,7 +540,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
539540
| [Lintcode-81. Find Median From Data Stream](https://www.lintcode.com/problem/find-median-from-data-stream) | [c++](./lintcode/81.find-median-from-data-stream.cpp), [python](./lintcode/81.find-median-from-data-stream.py) | \- | \- | \- | - |
540541
| [Lintcode-89. K Sum](https://www.lintcode.com/problem/k-sum) | [c++](./lintcode/89.k-sum.cpp), [python](./lintcode/89.k-sum.py) | \- | \- | \- | - |
541542
| [Lintcode-91. Minimum Adjustment Cost](https://www.lintcode.com/problem/minimum-adjustment-cost) | [c++](./lintcode/91.minimum-adjustment-cost.cpp), [python](./lintcode/91.minimum-adjustment-cost.py) | \- | \- | \- | - |
542-
| [Lintcode-125. Backpack II](https://www.lintcode.com/problem/backpack-ii) | [python](./lintcode/125.backpack-ii.py), [c++](./lintcode/125.backpack-ii.cpp) | \- | \- | \- | - |
543543
| [Lintcode-139. Subarray Sum Closest](https://www.lintcode.com/problem/subarray-sum-closest) | [c++](./lintcode/139.subarray-sum-closest.cpp), [python](./lintcode/139.subarray-sum-closest.py) | \- | \- | \- | - |
544544
| [Lintcode-144. Interleaving Positive And Negative Numbers](https://www.lintcode.com/problem/interleaving-positive-and-negative-numbers) | [python](./lintcode/144.interleaving-positive-and-negative-numbers.py), [c++](./lintcode/144.interleaving-positive-and-negative-numbers.cpp) | \- | \- | \- | - |
545545
| [Lintcode-183. Wood Cut](https://www.lintcode.com/problem/wood-cut) | [c++](./lintcode/183.wood-cut.cpp), [python](./lintcode/183.wood-cut.py) | \- | \- | \- | - |

lintcode/125.backpack-ii.cpp

+91-19
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,104 @@
1-
//f[i][w] = max(f[i - 1][w], f[i - 1][w - A[i - 1]] + V[i - 1])
1+
// Tag: Backpack DP, Dynamic Programming/DP
2+
// Time: O(N^2)
3+
// Space: O(N^2)
4+
// Ref: -
5+
// Note: -
6+
7+
// There are `n` items and a backpack with size `m`.
8+
// Given array `A` representing the size of each item and array `V` representing the value of each item.
9+
//
10+
// What's the maximum value can you put into the backpack?
11+
//
12+
// **Example 1:**
13+
//
14+
// Input:
15+
// ```
16+
// m = 10
17+
// A = [2, 3, 5, 7]
18+
// V = [1, 5, 2, 4]
19+
// ```
20+
// Output:
21+
// ```
22+
// 9
23+
// ```
24+
// Explanation:
25+
//
26+
// Put A[1] and A[3] into backpack, getting the maximum value V[1] + V[3] = 9
27+
//
28+
// **Example 2:**
29+
//
30+
// Input:
31+
// ```
32+
// m = 10
33+
// A = [2, 3, 8]
34+
// V = [2, 5, 8]
35+
// ```
36+
// Output:
37+
// ```
38+
// 10
39+
// ```
40+
// Explanation:
41+
//
42+
// Put A[0] and A[2] into backpack, getting the maximum value V[0] + V[2] = 10
43+
//
44+
// 1. `A[i], V[i], n, m` are all integers.
45+
// 2. You can not split an item.
46+
// 3. The sum size of the items you want to put into backpack can not exceed `m`.
47+
// 4. Each item can only be picked up once
48+
// 5. $m <= 1000$\
49+
// $len(A),len(V)<=100$
50+
51+
class Solution {
52+
public:
53+
/**
54+
* @param m: An integer m denotes the size of a backpack
55+
* @param a: Given n items with size A[i]
56+
* @param v: Given n items with value V[i]
57+
* @return: The maximum value
58+
*/
59+
int backPackII(int m, vector<int> &a, vector<int> &v) {
60+
// write your code here
61+
int n = a.size();
62+
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
63+
for (int i = 1; i <= n; i++) {
64+
for (int j = 1; j <= m; j++) {
65+
int w = a[i - 1];
66+
int val = v[i - 1];
67+
if (w <= j) {
68+
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + val);
69+
} else {
70+
dp[i][j] = dp[i - 1][j];
71+
}
72+
}
73+
}
74+
75+
return dp[n][m];
76+
}
77+
};
278

379
class Solution {
480
public:
581
/**
682
* @param m: An integer m denotes the size of a backpack
7-
* @param A: Given n items with size A[i]
8-
* @param V: Given n items with value V[i]
83+
* @param a: Given n items with size A[i]
84+
* @param v: Given n items with value V[i]
985
* @return: The maximum value
1086
*/
11-
int backPackII(int m, vector<int> &A, vector<int> &V) {
87+
int backPackII(int m, vector<int> &a, vector<int> &v) {
1288
// write your code here
13-
vector<int>table(m + 1, -1);
14-
table[0] = 0;
15-
16-
for(auto i = 0; i < A.size(); ++i)
17-
{
18-
for(auto w = m + 1; w >= 0; --w)
19-
{
20-
int value = table[w];
21-
22-
if (w - A[i] >= 0 && table[w - A[i]] != -1)
23-
{
24-
value = max(value, table[w - A[i]] + V[i]);
89+
int n = a.size();
90+
vector<int> dp(m + 1, 0);
91+
92+
for (int i = 1; i <= n; i++) {
93+
for (int j = m; j >= 1; j--) {
94+
int w = a[i - 1];
95+
int val = v[i - 1];
96+
if (w <= j) {
97+
dp[j] = max(dp[j], dp[j - w] + val);
2598
}
26-
table[w] = value;
2799
}
28100
}
29-
30-
return *max_element(table.begin(), table.end());
101+
102+
return dp[m];
31103
}
32104
};

lintcode/125.backpack-ii.py

+87-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,97 @@
1-
# f[i][w] = max(f[i - 1][w], f[i - 1][w - A[i - 1]] + v[i - 1])
1+
# Tag: Backpack DP, Dynamic Programming/DP
2+
# Time: O(N^2)
3+
# Space: O(N^2)
4+
# Ref: -
5+
# Note: -
6+
7+
# There are `n` items and a backpack with size `m`.
8+
# Given array `A` representing the size of each item and array `V` representing the value of each item.
9+
#
10+
# What's the maximum value can you put into the backpack?
11+
#
12+
# **Example 1:**
13+
#
14+
# Input:
15+
# ```
16+
# m = 10
17+
# A = [2, 3, 5, 7]
18+
# V = [1, 5, 2, 4]
19+
# ```
20+
# Output:
21+
# ```
22+
# 9
23+
# ```
24+
# Explanation:
25+
#
26+
# Put A[1] and A[3] into backpack, getting the maximum value V[1] + V[3] = 9
27+
#
28+
# **Example 2:**
29+
#
30+
# Input:
31+
# ```
32+
# m = 10
33+
# A = [2, 3, 8]
34+
# V = [2, 5, 8]
35+
# ```
36+
# Output:
37+
# ```
38+
# 10
39+
# ```
40+
# Explanation:
41+
#
42+
# Put A[0] and A[2] into backpack, getting the maximum value V[0] + V[2] = 10
43+
#
44+
# 1. `A[i], V[i], n, m` are all integers.
45+
# 2. You can not split an item.
46+
# 3. The sum size of the items you want to put into backpack can not exceed `m`.
47+
# 4. Each item can only be picked up once
48+
# 5. $m <= 1000$\
49+
# $len(A),len(V)<=100$
50+
51+
from typing import (
52+
List,
53+
)
54+
255
class Solution:
356
"""
457
@param m: An integer m denotes the size of a backpack
5-
@param A: Given n items with size A[i]
6-
@param V: Given n items with value V[i]
58+
@param a: Given n items with size A[i]
59+
@param v: Given n items with value V[i]
760
@return: The maximum value
861
"""
9-
def backPackII(self, m, A, V):
62+
def back_pack_i_i(self, m: int, a: List[int], v: List[int]) -> int:
1063
# write your code here
11-
table = [-1 for _ in range(m + 1)]
12-
table[0] = 0
64+
n = len(a)
65+
dp = [[0 for j in range(m + 1)] for i in range(n + 1)]
1366

14-
for i in range(len(A)):
15-
for w in range(m, -1, -1):
16-
value = table[w]
67+
for i in range(1, n + 1):
68+
for j in range(1, m + 1):
69+
w = a[i - 1]
70+
val = v[i - 1]
71+
if j >= w:
72+
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + val)
73+
else:
74+
dp[i][j] = dp[i - 1][j]
1775

18-
if w - A[i] >= 0 and table[w - A[i]] != -1:
19-
value = max(value, table[w - A[i]] + V[i])
76+
return dp[n][m]
77+
78+
class Solution:
79+
"""
80+
@param m: An integer m denotes the size of a backpack
81+
@param a: Given n items with size A[i]
82+
@param v: Given n items with value V[i]
83+
@return: The maximum value
84+
"""
85+
def back_pack_i_i(self, m: int, a: List[int], v: List[int]) -> int:
86+
# write your code here
87+
n = len(a)
88+
dp = [0 for j in range(m + 1)]
2089

21-
table[w] = value
90+
for i in range(1, n + 1):
91+
for j in range(m, 0, -1):
92+
w = a[i - 1]
93+
val = v[i - 1]
94+
if j >= w:
95+
dp[j] = max(dp[j], dp[j - w] + val)
2296

23-
return max(table)
97+
return dp[m]

lintcode/92.backpack.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tag: Backpack DP, Dynamic Programming/DP
2-
// Time: -
3-
// Space: -
2+
// Time: O(N^2)
3+
// Space: O(N^2)
44
// Ref: -
55
// Note: -
66

lintcode/92.backpack.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tag: Backpack DP, Dynamic Programming/DP
2-
# Time: -
3-
# Space: -
2+
# Time: O(N^2)
3+
# Space: O(N^2)
44
# Ref: -
55
# Note: -
66

0 commit comments

Comments
 (0)