Why this solution will lead to Time Limit Exceeded? Thanks!


  • -2
    D

    Why this solution will lead to Time Limit Exceeded? Thanks!

        vector<vector<int> > combine(int n, int k) {
    vector<int> num;
    for (int i=0;i<n;i++)
    {
        num.push_back(i+1);
        
    }
    vector<vector<int>> result;
    vector<int> path; 
    
    dfs(num, path, result, k);
    return result;      
        }
        
    void dfs(const vector<int>& num, vector<int> &path,vector<vector<int> > &result, int k) {
    if (path.size() == k) { 
    result.push_back(path);
    return;
    }
    
    for (auto i : num) {
    auto pos = find(path.begin(), path.end(), i);
    if (pos == path.end()) {
    path.push_back(i);
    dfs(num, path, result,k);
    path.pop_back();
    }
    }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.