Sanath12345

Untitled

Jun 28th, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. class Solution {
  2. public:
  3. void help(int i,vector<int> &c,vector<vector<int>> &S,int t,vector<int> &cur){
  4. if(t<0) return ;
  5. if(t==0){
  6. S.push_back(cur);
  7. return ;
  8. }
  9. if(i==int(c.size())) return ;
  10. cur.push_back(c[i]);
  11. help(i+1,c,S,t-c[i],cur);
  12. cur.pop_back();
  13. int j=i;
  14. while(j<int(c.size()) and c[i]==c[j]) j++;
  15. help(j,c,S,t,cur);
  16. }
  17. vector<vector<int>> combinationSum2(vector<int>& c, int t) {
  18. sort(c.begin(),c.end());
  19. vector<vector<int>> ans;
  20. vector<int> cur;
  21. help(0,c,ans,t,cur);
  22. return ans;
  23. }
  24. };
  25.  
  26. i
  27. 1,2,2,2,5 target = 5
  28.  
  29. ith index -> put ith index element in {} || leave it and move to next index
  30.  
  31.  
  32. {1} sum=1
  33. {1,2} sum=3
  34. {1,2,2} sum=5
  35. {1,2} sum=3
  36. {1,2,5} sum=8
  37. {1,2} sum=3
  38.  
  39.  
  40. 1,2,3
  41.  
  42. 0th index -> option1 , option2 pending
  43. {1}
  44. 1st index -> option1 , option2 pending
  45. {1,2}
  46. 2nd index -> option1, option2 pending
  47. {1,2,3}
  48. 2nd index -> option2
  49. {1,2}
  50. 1st index -> option2
  51. {1}
  52. 2nd index -> option1 ,option2 pending
  53. {1,3}
  54. 2nd index -> option2
  55. {1}
  56. 0th index -> option2
  57. {}
  58. 1st index -> option1, option2 pending
  59. {2}
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment