My code is time exceeded


  • 0
    W
    public class Solution {
    public boolean[] isUsed;
    List<List<Integer>> resultList;
    ArrayList<Integer> innerList;
    
    public List<List<Integer>> combine(int n, int k) {
        isUsed = new boolean[n];
        resultList = new ArrayList<List<Integer>>();
        innerList = new ArrayList<Integer>();
        int count = 0;
        getCombos(n, k, count);
        return resultList;
    }
    
    public void getCombos(int n, int k, int count){
        //base case
        if(k == 0){
            resultList.add((ArrayList<Integer>)innerList.clone());
            return;
        }
        
        for(int i = 0; i < n; ++i){
            if(!isUsed[i]){
                isUsed[i] = true;
                innerList.add(i + 1);
                getCombos(n, k - 1, count + 1);
                isUsed[i] = false;
                innerList.remove(count);
            }
        }
    }
    

    }

    My code actually uses recursive until it hits the kth number.


Log in to reply
 

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