Skip to content

Commit ee1fd4c

Browse files
committed
0377-combination-sum-iv.md Added 7 languages' solutions.
1 parent 11090be commit ee1fd4c

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ After finishing one category of questions, you can study another category to imp
2525
- [72. Edit Distance](problems/0072-edit-distance.md)
2626

2727
### Knapsack problems
28+
#### 0/1 Knapsack
2829
- [416. Partition Equal Subset Sum](problems/0416-partition-equal-subset-sum.md)
2930
- [1049. Last Stone Weight II](problems/1049-last-stone-weight-ii.md)
3031
- [494. Target Sum](problems/0494-target-sum.md)
3132
- [474. Ones and Zeroes](problems/0474-ones-and-zeroes.md)
33+
34+
#### Unbounded Knapsack
3235
- [518. Coin Change II](problems/0518-coin-change-ii.md)
36+
- [377. Combination Sum IV](problems/0377-combination-sum-iv.md)
37+
3338

3439
- More LeetCode problems will be added soon...

problems/0377-combination-sum-iv.md

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# 377. Combination Sum IV
2+
LeetCode problem: [377. Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)
3+
4+
## LeetCode problem description
5+
> Given an array of **distinct** integers `nums` and a target integer `target`, return the number of possible combinations that add up to `target`.
6+
7+
The test cases are generated so that the answer can fit in a 32-bit integer.
8+
9+
```
10+
Example 1:
11+
12+
Input: nums = [1,2,3], target = 4
13+
Output: 7
14+
15+
Explanation:
16+
The possible combination ways are:
17+
(1, 1, 1, 1)
18+
(1, 1, 2)
19+
(1, 2, 1)
20+
(1, 3)
21+
(2, 1, 1)
22+
(2, 2)
23+
(3, 1)
24+
Note that different sequences are counted as different combinations.
25+
------------------------------------------------------------------------
26+
27+
Example 2:
28+
29+
Input: nums = [9], target = 3
30+
Output: 0
31+
32+
------------------------------------------------------------------------
33+
34+
Constraints:
35+
36+
1 <= nums.length <= 200
37+
1 <= nums[i] <= 1000
38+
All the elements of 'nums' are 'unique'.
39+
1 <= target <= 1000
40+
```
41+
42+
## Thoughts
43+
This is `Unbounded Knapsack Problem` A, and it also requires us to consider the sequences.
44+
45+
### Complexity
46+
* Time: `O(n * m)`.
47+
* Space: `O(n)`.
48+
49+
## Python
50+
```python
51+
class Solution:
52+
def combinationSum4(self, nums: List[int], target: int) -> int:
53+
dp = [0] * (target + 1)
54+
dp[0] = 1
55+
56+
for i in range(1, len(dp)):
57+
for num in nums:
58+
if i >= num:
59+
dp[i] += dp[i - num]
60+
61+
return dp[-1]
62+
```
63+
64+
## C++
65+
```cpp
66+
class Solution {
67+
public:
68+
int combinationSum4(vector<int>& nums, int target) {
69+
vector<unsigned int> dp(target + 1, 0);
70+
dp[0] = 1;
71+
72+
for (auto i = 1; i < dp.size(); i++) {
73+
for (auto num : nums) {
74+
if (i >= num) {
75+
dp[i] += dp[i - num];
76+
}
77+
}
78+
}
79+
80+
return dp.back();
81+
}
82+
};
83+
```
84+
85+
## Java
86+
```java
87+
class Solution {
88+
public int combinationSum4(int[] nums, int target) {
89+
var dp = new int[target + 1];
90+
dp[0] = 1;
91+
92+
for (var i = 1; i < dp.length; i++) {
93+
for (var num : nums) {
94+
if (i >= num) {
95+
dp[i] += dp[i - num];
96+
}
97+
}
98+
}
99+
100+
return dp[target];
101+
}
102+
}
103+
```
104+
105+
## C#
106+
```c#
107+
public class Solution {
108+
public int CombinationSum4(int[] nums, int target) {
109+
var dp = new int[target + 1];
110+
dp[0] = 1;
111+
112+
for (var i = 1; i < dp.Length; i++) {
113+
foreach (var num in nums) {
114+
if (i >= num) {
115+
dp[i] += dp[i - num];
116+
}
117+
}
118+
}
119+
120+
return dp[target];
121+
}
122+
}
123+
```
124+
125+
## JavaScript
126+
```javascript
127+
var combinationSum4 = function(nums, target) {
128+
const dp = Array(target + 1).fill(0)
129+
dp[0] = 1
130+
131+
for (let i = 1; i < dp.length; i++) {
132+
for (const num of nums) {
133+
if (i >= num) {
134+
dp[i] += dp[i - num]
135+
}
136+
}
137+
}
138+
139+
return dp.at(-1)
140+
};
141+
```
142+
143+
## Go
144+
```go
145+
func combinationSum4(nums []int, target int) int {
146+
dp := make([]int, target + 1)
147+
dp[0] = 1
148+
149+
for i := 1; i < len(dp); i++ {
150+
for _, num := range nums {
151+
if i >= num {
152+
dp[i] += dp[i - num]
153+
}
154+
}
155+
}
156+
157+
return dp[target]
158+
}
159+
```
160+
161+
## Ruby
162+
```ruby
163+
def combination_sum4(nums, target)
164+
dp = Array.new(target + 1, 0)
165+
dp[0] = 1
166+
167+
(1...dp.size).each do |i|
168+
nums.each do |num|
169+
dp[i] += dp[i - num] if i >= num
170+
end
171+
end
172+
173+
return dp[-1]
174+
end
175+
```

0 commit comments

Comments
 (0)