Skip to content

Commit 855f6b7

Browse files
committed
update 563
1 parent 7641266 commit 855f6b7

File tree

5 files changed

+205
-54
lines changed

5 files changed

+205
-54
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LeetCode solutions, written in python and cpp
1010

1111
- [9c-advanced.md](list/9c-advanced.md) 60/87
1212
- [9c-basic.md](list/9c-basic.md) 81/130
13-
- [9c-dp.md](list/9c-dp.md) 23/23
13+
- [9c-dp.md](list/9c-dp.md) 26/26
1414
- [9c-top.md](list/9c-top.md) 25/49
1515
- [geekbang.md](list/geekbang.md) 55/55
1616
- [lee215.md](list/lee215.md) 2/34
@@ -189,7 +189,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
189189

190190
## Dynamic Programming
191191

192-
| Link | Problem(57) | Solution | Tag | Time | Space | Note | Ref |
192+
| Link | Problem(58) | Solution | Tag | Time | Space | Note | Ref |
193193
| ----- | ----- | ----- | ----- | ----- | ----- | ----- | ----- |
194194
| [leetcode-542](https://leetcode.com/problems/01-matrix/) | 01 Matrix | [c++](./leetcode/542.01-matrix.cpp), [python3](./leetcode/542.01-matrix.py) | Dynamic Programming | O\(MN\) | O\(MN\) | \- | - |
195195
| [leetcode-650](https://leetcode.com/problems/2-keys-keyboard/) | 2 Keys Keyboard | [c++](./leetcode/650.2-keys-keyboard.cpp), [python3](./leetcode/650.2-keys-keyboard.py) | Dynamic Programming | O\(NlogN\) | O\(N\) | \- | - |
@@ -244,6 +244,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
244244
| [lintcode-125](https://www.lintcode.com/problem/backpack-ii/) | Backpack II | [c++](./lintcode/125.backpack-ii.cpp), [python3](./lintcode/125.backpack-ii.py) | Dynamic Programming | O\(N^2\) | O\(N^2\) | \- | - |
245245
| [lintcode-440](https://www.lintcode.com/problem/backpack-iii/) | Backpack III | [c++](./lintcode/440.backpack-iii.cpp), [python3](./lintcode/440.backpack-iii.py) | Dynamic Programming | O\(N^2\) | O\(N^2\) | \- | - |
246246
| [lintcode-562](https://www.lintcode.com/problem/backpack-iv/) | Backpack IV | [c++](./lintcode/562.backpack-iv.cpp), [python3](./lintcode/562.backpack-iv.py) | Dynamic Programming | O\(N^2\) | O\(N^2\) | \- | - |
247+
| [lintcode-563](https://www.lintcode.com/problem/backpack-v/) | Backpack V | [c++](./lintcode/563.backpack-v.cpp), [python3](./lintcode/563.backpack-v.py) | Dynamic Programming | O\(N^2\) | O\(N\) | \- | - |
247248
| [lintcode-394](https://www.lintcode.com/problem/coins-in-a-line/) | Coins In A Line | [c++](./lintcode/394.coins-in-a-line.cpp), [python3](./lintcode/394.coins-in-a-line.py) | Dynamic Programming | O\(N\) | O\(N\) | \- | - |
248249
| [lintcode-437](https://www.lintcode.com/problem/copy-books/) | Copy Books | [c++](./lintcode/437.copy-books.cpp), [python3](./lintcode/437.copy-books.py) | Dynamic Programming | O\(N\*logP\) | O\(1\) | Answer \| DP | - |
249250
| [lintcode-515](https://www.lintcode.com/problem/paint-house/) | Paint House | [c++](./lintcode/515.paint-house.cpp), [python3](./lintcode/515.paint-house.py) | Dynamic Programming | O\(N\) | O\(N\) | Leetcode\-256 | - |
@@ -724,7 +725,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
724725

725726
## Other
726727

727-
| Link | Problem(171) | Solution | Tag | Time | Space | Note | Ref |
728+
| Link | Problem(170) | Solution | Tag | Time | Space | Note | Ref |
728729
| ----- | ----- | ----- | ----- | ----- | ----- | ----- | ----- |
729730
| [leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | \- | - |
730731
| [leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | \- | - |
@@ -873,7 +874,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
873874
| [leetcode-127](https://leetcode.com/problems/word-ladder/) | Word Ladder | [c++](./leetcode/127.word-ladder.cpp), [python3](./leetcode/127.word-ladder.py) | Other | \- | \- | \- | - |
874875
| [leetcode-425](https://leetcode.com/problems/word-squares/) | Word Squares | [c++](./leetcode/425.word-squares.cpp), [python3](./leetcode/425.word-squares.py) | Other | \- | \- | \- | - |
875876
| [leetcode-6](https://leetcode.com/problems/zigzag-conversion/) | Zigzag Conversion | [python3](./leetcode/6.zigzag-conversion.py) | Other | \- | \- | \- | - |
876-
| [lintcode-563](https://www.lintcode.com/problem/backpack-v/) | Backpack V | [c++](./lintcode/563.backpack-v.cpp), [python3](./lintcode/563.backpack-v.py) | Other | \- | \- | \- | - |
877877
| [lintcode-396](https://www.lintcode.com/problem/coins-in-a-line-iii/) | Coins In A Line III | [c++](./lintcode/396.coins-in-a-line-iii.cpp), [python3](./lintcode/396.coins-in-a-line-iii.py) | Other | \- | \- | \- | - |
878878
| [lintcode-589](https://www.lintcode.com/problem/connecting-graph/) | Connecting Graph | [c++](./lintcode/589.connecting-graph.cpp), [python3](./lintcode/589.connecting-graph.py) | Other | \- | \- | \- | - |
879879
| [lintcode-590](https://www.lintcode.com/problem/connecting-graph-ii/) | Connecting Graph II | [c++](./lintcode/590.connecting-graph-ii.cpp), [python3](./lintcode/590.connecting-graph-ii.py) | Other | \- | \- | \- | - |

lintcode/563.backpack-v.cpp

+79-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1-
// f[i][w] = f[i - 1][w] + f[i - 1][w - A[i - 1]]
1+
// Tag: Backpack DP, Dynamic Programming/DP
2+
// Time: O(N^2)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given n items with size `nums[i]` which an integer array and all positive numbers.
8+
// An integer `target` denotes the size of a backpack.
9+
// Find the number of possible ways to fill the backpack.
10+
// `Each item may only be used once`
11+
//
12+
// Example 1:
13+
// ```
14+
// Input:
15+
// nums = [1,2,3,3,7]
16+
// target = 7
17+
// Output:
18+
// 2
19+
// Explanation:
20+
// The resulting set is:
21+
// [7]
22+
// [1,3,3]
23+
// Returns 2
24+
// ```
25+
//
26+
// Example 2:
27+
// ```
28+
// Input:
29+
// nums = [1,1,1,1]
30+
// target = 3
31+
// Output:
32+
// 4
33+
// Explanation:
34+
// Choose 3 out of 4 items, 4 choices in total
35+
// ```
36+
//
37+
// $1 \leq nums.length \leq 1000$
38+
// $1 \leq target \leq 1000$
39+
240
class Solution {
341
public:
442
/**
@@ -8,22 +46,49 @@ class Solution {
846
*/
947
int backPackV(vector<int> &nums, int target) {
1048
// write your code here
11-
vector<int> table(target + 1, 0);
12-
table[0] = 1;
13-
14-
for (auto i = 0; i < nums.size(); ++i)
15-
{
16-
for (auto w = target; w >=0; --w)
17-
{
18-
int value = table[w];
19-
if (w - nums[i] >= 0)
20-
{
21-
value += table[w - nums[i]];
49+
int n = nums.size();
50+
vector<vector<int>> dp(n + 1, vector<int>(target + 1, 0));
51+
52+
for (int i = 0; i <= n; i++) {
53+
dp[i][0] = 1;
54+
}
55+
56+
for (int i = 1; i <= n; i++) {
57+
for (int j = 1; j <= target; j++) {
58+
int weight = nums[i - 1];
59+
dp[i][j] = dp[i - 1][j];
60+
if (j >= weight) {
61+
dp[i][j] += dp[i - 1][j - weight];
62+
}
63+
}
64+
}
65+
66+
return dp[n][target];
67+
}
68+
};
69+
70+
class Solution {
71+
public:
72+
/**
73+
* @param nums: an integer array and all positive numbers
74+
* @param target: An integer
75+
* @return: An integer
76+
*/
77+
int backPackV(vector<int> &nums, int target) {
78+
// write your code here
79+
int n = nums.size();
80+
vector<int> dp(target + 1, 0);
81+
dp[0] = 1;
82+
83+
for (int i = 1; i <= n; i++) {
84+
for (int j = target; j >= 1; j--) {
85+
int weight = nums[i - 1];
86+
if (j >= weight) {
87+
dp[j] += dp[j - weight];
2288
}
23-
table[w] = value;
2489
}
2590
}
2691

27-
return table[target];
92+
return dp[target];
2893
}
2994
};

lintcode/563.backpack-v.py

+99-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,106 @@
1-
# f[i][w] = f[i - 1][w] + f[i - 1][w - A[i - 1]]
1+
# Tag: Backpack DP, Dynamic Programming/DP
2+
# Time: O(N^2)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given n items with size `nums[i]` which an integer array and all positive numbers.
8+
# An integer `target` denotes the size of a backpack.
9+
# Find the number of possible ways to fill the backpack.
10+
# `Each item may only be used once`
11+
#
12+
# Example 1:
13+
# ```
14+
# Input:
15+
# nums = [1,2,3,3,7]
16+
# target = 7
17+
# Output:
18+
# 2
19+
# Explanation:
20+
# The resulting set is:
21+
# [7]
22+
# [1,3,3]
23+
# Returns 2
24+
# ```
25+
#
26+
# Example 2:
27+
# ```
28+
# Input:
29+
# nums = [1,1,1,1]
30+
# target = 3
31+
# Output:
32+
# 4
33+
# Explanation:
34+
# Choose 3 out of 4 items, 4 choices in total
35+
# ```
36+
#
37+
# $1 \leq nums.length \leq 1000$
38+
# $1 \leq target \leq 1000$
39+
40+
from typing import (
41+
List,
42+
)
43+
44+
class Solution:
45+
"""
46+
@param nums: an integer array and all positive numbers
47+
@param target: An integer
48+
@return: An integer
49+
"""
50+
def back_pack_v(self, nums: List[int], target: int) -> int:
51+
# write your code here
52+
n = len(nums)
53+
dp = [[0] * (target + 1) for i in range(n + 1)]
54+
55+
for i in range(n + 1):
56+
dp[i][0] = 1
57+
58+
for i in range(1, n + 1):
59+
for j in range(1, target + 1):
60+
weight = nums[i - 1]
61+
dp[i][j] = dp[i - 1][j]
62+
if j >= weight:
63+
dp[i][j] += dp[i - 1][j - weight]
64+
65+
return dp[n][target]
66+
267
class Solution:
368
"""
469
@param nums: an integer array and all positive numbers
570
@param target: An integer
671
@return: An integer
772
"""
8-
def backPackV(self, nums, target):
73+
def back_pack_v(self, nums: List[int], target: int) -> int:
974
# write your code here
10-
table = [0 for _ in range(target + 1)]
11-
table[0] = 1
12-
13-
for i in range(len(nums)):
14-
for w in range(target, -1, -1):
15-
value = table[w]
16-
if w - nums[i] >= 0:
17-
value += table[w - nums[i]]
18-
19-
table[w] = value
20-
21-
return table[-1]
75+
n = len(nums)
76+
dp = [0] * (target + 1)
77+
dp[0] = 1
78+
79+
for i in range(1, n + 1):
80+
for j in range(1, target + 1):
81+
weight = nums[i - 1]
82+
if j >= weight:
83+
dp[i] += dp[i - 1][j - weight]
84+
85+
return [target]
86+
87+
class Solution:
88+
"""
89+
@param nums: an integer array and all positive numbers
90+
@param target: An integer
91+
@return: An integer
92+
"""
93+
def back_pack_v(self, nums: List[int], target: int) -> int:
94+
# write your code here
95+
n = len(nums)
96+
dp = [0] * (target + 1)
97+
dp[0] = 1
98+
99+
for i in range(1, n + 1):
100+
for j in range(target, 0, -1):
101+
weight = nums[i - 1]
102+
if j >= weight:
103+
dp[j] += dp[j - weight]
104+
105+
return dp[target]
106+

list/9c-dp.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636
21. https://leetcode.com/problems/palindrome-partitioning-ii/
3737
22. https://www.lintcode.com/problem/copy-books/
3838
23. https://www.lintcode.com/problem/coins-in-a-line/
39-
40-
* https://www.lintcode.com/problem/backpack/
41-
* https://www.lintcode.com/problem/backpack-v/
42-
* https://www.lintcode.com/problem/backpack-iv/
39+
24. https://www.lintcode.com/problem/backpack/
40+
25. https://www.lintcode.com/problem/backpack-v/
41+
26. https://www.lintcode.com/problem/backpack-iv/
4342

4443

4544

test/python.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@
88
List,
99
)
1010

11+
from typing import (
12+
List,
13+
)
14+
1115
class Solution:
1216
"""
13-
@param n: An integer
14-
@return: A boolean which equals to true if the first player will win
17+
@param nums: an integer array and all positive numbers
18+
@param target: An integer
19+
@return: An integer
1520
"""
16-
def first_will_win(self, n: int) -> bool:
21+
def back_pack_v(self, nums: List[int], target: int) -> int:
1722
# write your code here
18-
if n == 0:
19-
return False
20-
21-
if n <= 2:
22-
return True
23+
n = len(nums)
24+
dp = [0] * (target + 1)
25+
dp[0] = 1
2326

24-
dp = [False for i in range(1 + n)]
25-
dp[1] = dp[2] = True
27+
for i in range(1, n + 1):
28+
tmp = list(dp)
29+
for j in range(1, target + 1):
30+
weight = nums[i - 1]
31+
if j >= weight:
32+
tmp[j] = dp[j] + dp[j - weight]
2633

27-
for i in range(3, n + 1):
28-
dp[i] = not (dp[i - 1] or dp[i - 2])
29-
30-
return dp[n]
34+
dp = tmp
35+
return dp[target]
3136

3237

3338

3439
s = Solution()
35-
res = s.first_will_win(0)
36-
print(res)
37-
38-
res = s.first_will_win(11)
40+
res = s.back_pack_v([1,2,3,3,7], 7)
3941
print(res)

0 commit comments

Comments
 (0)