Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<int>> res;
- vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
- vector<int> path;
- sort(candidates.rbegin(), candidates.rend());
- backtrack(candidates, target, 0, path);
- return res;
- }
- void backtrack(vector<int> &cand, int sum, int idx, vector<int> &path){
- if(sum == 0){
- res.push_back(path);
- return;
- }
- for(int i=idx; i<cand.size(); i++){
- if(sum-cand[i] >= 0){
- path.push_back(cand[i]);
- sum-=cand[i];
- backtrack(cand, sum, i, path);
- path.pop_back();
- sum+=cand[i];
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment