[InterviewBit] Combinations

Combinations

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void helper(vector<vector<int>>& res, int now, int n, int rem, vector<int>& A) {
if(rem == 0) res.push_back(A);
else {
A.push_back(now);
helper(res, now + 1, n, rem - 1, A);
A.pop_back();
if(n - now >= rem) helper(res,now+1,n,rem,A);
}
}
vector<vector<int>> Solution::combine(int A, int B) {
if(A < B) return {};
vector<vector<int>> res;
helper(res, 1, A, B, vector<int>() = {});
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/22/PS/interviewbit/combinations/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.