Why My C++ dfs solution TLE?


  • 0
    R

    why my solution TLE???

    void dfs(vector<vector<int>>& ans, vector<int>& com, int n, int k, int start){
        if(k == 0){
            ans.push_back(com);
            return;
        }
        
        for(int i = start; i < n; i++){
            com.push_back(i+1);
            dfs(ans, com, n, k-1, start+1);
            com.pop_back();
        }
    }
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> ans;
        if(n < 1) return ans;
        vector<int> com;
        dfs(ans, com, n, k, 0);
        return ans;
    }

  • 0
    L

    Did you test your program? your program is not the right answer....


Log in to reply
 

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