Hi. I am trying to solve Combination sum problem https://leetcode.com/problems/combination-sum/
I am considering 3 cases. For each index
- Pick and remain in same index
- Pick and go to next index
- Dont pick and go to next
But i am stuck with the implementation of that. Can someone please help me with it?
This is what i have tried till now https://pastebin.pl/view/63f6a688
Thanks in advanced
View
class Solution {
public:
vector a;
bool f(int p,int left,vector& c,vector& ans)
{
if(left<0) return 0;
if(left==0)
return 1;
//vector a;
if(f(p,left-c[p],c,ans)) a.push_back(c[p]);
f(p+1,left-c[p],c,ans);
f(p+1,left,c,ans);
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> x;
f(0,target,candidates,x);
return x;
}
};