Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6233926

Browse files
backtrack
1 parent 9d717fa commit 6233926

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Source : https://leetcode.com/problems/combination-sum/
2+
// Author : henrytien
3+
// Date : 2022年02月17日
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given an array of distinct integers candidates and a target integer target, return a list of all
8+
* unique combinations of candidates where the chosen numbers sum to target. You may return the
9+
* combinations in any order.
10+
*
11+
* The same number may be chosen from candidates an unlimited number of times. Two combinations are
12+
* unique if the frequency of at least one of the chosen numbers is different.
13+
*
14+
* It is guaranteed that the number of unique combinations that sum up to target is less than 150
15+
* combinations for the given input.
16+
*
17+
* Example 1:
18+
*
19+
* Input: candidates = [2,3,6,7], target = 7
20+
* Output: [[2,2,3],[7]]
21+
* Explanation:
22+
* 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
23+
* 7 is a candidate, and 7 = 7.
24+
* These are the only two combinations.
25+
*
26+
* Example 2:
27+
*
28+
* Input: candidates = [2,3,5], target = 8
29+
* Output: [[2,2,2,2],[2,3,3],[3,5]]
30+
*
31+
* Example 3:
32+
*
33+
* Input: candidates = [2], target = 1
34+
* Output: []
35+
*
36+
* Constraints:
37+
*
38+
* 1 <= candidates.length <= 30
39+
* 1 <= candidates[i] <= 200
40+
* All elements of candidates are distinct.
41+
* 1 <= target <= 500
42+
******************************************************************************************************/
43+
44+
#include "../inc/ac.h"
45+
46+
// Let N be the number of candidates, T be the target value, and M be the minimal value among the candidates.
47+
// Time Complexity: O(N^T/M)
48+
// Space Complexity: O(T/M)
49+
class Solution
50+
{
51+
public:
52+
vector<vector<int>> combinationSum(vector<int> &candidates, int target)
53+
{
54+
vector<vector<int>> res;
55+
vector<int> cur_res;
56+
backtrack(candidates,0,target,cur_res,res);
57+
return res;
58+
}
59+
60+
private:
61+
void backtrack(const vector<int> &canditates, int index, int remain,
62+
vector<int> &cur_res, vector<vector<int>> &res)
63+
{
64+
if (remain == 0)
65+
{
66+
res.push_back(cur_res);
67+
return;
68+
}else if(remain < 0) {
69+
// exceed the scope, stop exploration.
70+
return;
71+
}
72+
73+
for (int i = index; i < canditates.size(); i++)
74+
{
75+
if (remain >= canditates[i])
76+
{
77+
// Add the number into the combination
78+
cur_res.push_back(canditates[i]);
79+
backtrack(canditates, i, remain - canditates[i], cur_res, res);
80+
// backtrack, remove the number from the combination
81+
cur_res.pop_back();
82+
}
83+
}
84+
return;
85+
}
86+
};
87+
88+
int main()
89+
{
90+
91+
vector<int> candidates{2, 3, 6, 7};
92+
int target = 7;
93+
vector<vector<int>> res = Solution().combinationSum(candidates, target);
94+
for (auto &&iter : res)
95+
{
96+
print_vec(iter);
97+
}
98+
99+
return 0;
100+
}

‎leetcode/inc/ac.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ void print(ListNode *node){
5555
node = node->next;
5656
}
5757
}
58+
void print_vec(const vector<int>& vec){
59+
for(auto &&iter: vec)
60+
cout << iter << " ";
61+
cout << endl;
62+
}
5863
#endif /* ac_h */

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /