Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- void help(int i,vector<int> &c,vector<vector<int>> &S,int t,vector<int> &cur){
- if(t<0) return ;
- if(t==0){
- S.push_back(cur);
- return ;
- }
- if(i==int(c.size())) return ;
- cur.push_back(c[i]);
- help(i+1,c,S,t-c[i],cur);
- cur.pop_back();
- int j=i;
- while(j<int(c.size()) and c[i]==c[j]) j++;
- help(j,c,S,t,cur);
- }
- vector<vector<int>> combinationSum2(vector<int>& c, int t) {
- sort(c.begin(),c.end());
- vector<vector<int>> ans;
- vector<int> cur;
- help(0,c,ans,t,cur);
- return ans;
- }
- };
- i
- 1,2,2,2,5 target = 5
- ith index -> put ith index element in {} || leave it and move to next index
- {1} sum=1
- {1,2} sum=3
- {1,2,2} sum=5
- {1,2} sum=3
- {1,2,5} sum=8
- {1,2} sum=3
- 1,2,3
- 0th index -> option1 , option2 pending
- {1}
- 1st index -> option1 , option2 pending
- {1,2}
- 2nd index -> option1, option2 pending
- {1,2,3}
- 2nd index -> option2
- {1,2}
- 1st index -> option2
- {1}
- 2nd index -> option1 ,option2 pending
- {1,3}
- 2nd index -> option2
- {1}
- 0th index -> option2
- {}
- 1st index -> option1, option2 pending
- {2}
Advertisement
Add Comment
Please, Sign In to add comment