Problem: Given two integers n and k , return all possible combinations of k numbers out of 1 … n .
class Solution {
vector<vector<int>> ans;
void comb(int i, int n, int k, vector<int> path) {
if(path.size() == k)
{
ans.push_back(path);
return;
}
if(i > n)
return;
comb(i+1,n,k,path);
path.push_back(i);
comb(i+1,n,k,path);
}
public:
vector<vector<int>> combine(int n, int k) {
vector<int> dummy;
comb(1,n,k,dummy);
return ans;
}
};
This is my code, which gives TLE for the input n = 20 and k = 16. Can someone explain why and how to optimse this?