2014年5月10日星期六

Recursive Solution - Combinations

There are two solutions below, both share the same idea: use one element to append recursive call remaining. The second use a hash set and sort function to avoid duplicates, and then translate to results, the first one use ascending call plus a base to only find the effective results, which save a lot of time and resources. (second need to make sure only ascending results and keep the k > n check since in the recursive call stack, there is a chance k > n!)

public class Solution {
    public ArrayList<ArrayList<Integer>> combine(int n, int k) {
        if(k > n){
            return new ArrayList<ArrayList<Integer>>();
        }
        //return translate(combineOps(n, k));
     
        return comOperation(n, k, 0);
    }
 
    private ArrayList<ArrayList<Integer>> comOperation(int n, int k , int base){
     
        ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
        if(k == 0 || k > n){
            return results;
        }
     
        if(k == 1){
            for(int i = 1; i <= n; i++){
                ArrayList<Integer> result = new ArrayList<Integer>();
                result.add(i + base);
                results.add(result);
            }
            return results;
        }
        for(int i = 1; i <= n - k + 1; i++){
            ArrayList<ArrayList<Integer>> pre = comOperation(n - i, k - 1, i + base); // i + base
            for(ArrayList<Integer> lp : pre){
                ArrayList<Integer> clone = new ArrayList<Integer>();
                clone.addAll(lp);
                clone.add(0, i + base);
                results.add(clone);
            }
        }
        return results;
    }
 
 
    // private ArrayList<ArrayList<Integer>> translate(HashSet<ArrayList<Integer>> inputs){
    //     ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
    //     Iterator it = inputs.iterator();
    //     while(it.hasNext()){
    //         results.add((ArrayList<Integer>) it.next());
    //     }
    //     return results;
    // }
 
    // private HashSet<ArrayList<Integer>> combineOps(int n, int k){
    //     HashSet<ArrayList<Integer>> results = new HashSet<ArrayList<Integer>>();
     
    //     if(k == 0){
    //         return results;
    //     }
     
    //     if(k == 1){
    //         for(int i = 1; i <= n; i++){
    //             ArrayList<Integer> result = new ArrayList<Integer>();
    //             result.add(i);
    //             results.add(result);
    //         }
    //         return results;
    //     }
    //     HashSet<ArrayList<Integer>> preResults = combineOps(n, k - 1);
    //     for(int i = 1; i <= n; i++){
    //         for(ArrayList<Integer> lp : preResults){
    //             if(!lp.contains(i)){
    //                 ArrayList<Integer> clone = new ArrayList<Integer>();
    //                 clone.addAll(lp);
    //                 clone.add(i);
    //                 Collections.sort(clone);
    //                 results.add(clone);
    //             }
    //         }
    //     }
    //     return results;
    // }
}

没有评论: