-
Notifications
You must be signed in to change notification settings - Fork 2
/
377.CombinationSumIv.cpp
82 lines (75 loc) · 1.9 KB
/
377.CombinationSumIv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* @lc app=leetcode id=377 lang=cpp
*
* [377] Combination Sum IV
*
* https://leetcode.com/problems/combination-sum-iv/description/
*
* algorithms
* Medium (45.51%)
* Likes: 1721
* Dislikes: 208
* Total Accepted: 140.2K
* Total Submissions: 306.9K
* Testcase Example: '[1,2,3]\n4'
*
* Given an integer array with all positive numbers and no duplicates, find the
* number of possible combinations that add up to a positive integer target.
*
* Example:
*
*
* nums = [1, 2, 3]
* target = 4
*
* The possible combination ways are:
* (1, 1, 1, 1)
* (1, 1, 2)
* (1, 2, 1)
* (1, 3)
* (2, 1, 1)
* (2, 2)
* (3, 1)
*
* Note that different sequences are counted as different combinations.
*
* Therefore the output is 7.
*
*
*
*
* Follow up:
* What if negative numbers are allowed in the given array?
* How does it change the problem?
* What limitation we need to add to the question to allow negative numbers?
*
* Credits:
* Special thanks to @pbrother for adding this problem and creating all test
* cases.
*
*/
// @lc code=start
#include <vector>
class Solution {
public:
int combinationSum4(std::vector<int>& nums, int target) {
// memo[idx] = n means there are n subsets of nums sum up to idx
std::vector<unsigned long> memo(target + 1, 0);
// only empty set sums up to 0
memo[0] = 1;
// iterate each target
for (int i = 0; i <= target; i++) {
// for each target iterate all num in nums and
// use memo to find the result of current target
for (const int& num: nums) {
// if current target is greater than num
// then memo[i] could use all subsets belong to the memo[i - m] and add num
if (i >= num) {
memo[i] += memo[i - num];
}
}
}
return memo[target];
}
};
// @lc code=end