Advertisement
Guest User

Untitled

a guest
Oct 7th, 2016
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Stack;
  5.  
  6. public class Solution {
  7.     public static void main(String[] args) {
  8.         List<List<Integer>> combos = combinationSum(new int[] { 8, 7, 4, 3 }, 11);
  9.  
  10.         for (List<Integer> combo : combos) {
  11.             System.out.println(combo);
  12.         }
  13.     }
  14.  
  15.     public static List<List<Integer>> combinationSum(int[] candidates, int target) {
  16.         int[] candidatesCopy = candidates.clone();
  17.         Arrays.sort(candidatesCopy);
  18.  
  19.         Boolean[][] cache = new Boolean[target + 1][candidates.length];
  20.         boolean works = combinationSum(candidatesCopy, target, 0, cache);
  21.         List<List<Integer>> results = new ArrayList<>();
  22.  
  23.         if (!works) {
  24.             return results;
  25.         }
  26.  
  27.         generateSumOptions(candidatesCopy, target, 0, cache, results, new Stack<Integer>());
  28.  
  29.         return results;
  30.     }
  31.  
  32.     private static boolean combinationSum(int[] candidates, int target, int denominationIndex, Boolean[][] cache) {
  33.         if (target < 0) {
  34.             return false;
  35.         }
  36.  
  37.         if (target == 0) {
  38.             return true;
  39.         }
  40.  
  41.         if (denominationIndex == candidates.length) {
  42.             return false;
  43.         }
  44.  
  45.         if (cache[target][denominationIndex] != null) {
  46.             return cache[target][denominationIndex];
  47.         }
  48.  
  49.         if (denominationIndex > 0 && candidates[denominationIndex] == candidates[denominationIndex - 1]) {
  50.             cache[target][denominationIndex] = combinationSum(candidates, target, denominationIndex + 1, cache);
  51.             return cache[target][denominationIndex];
  52.         }
  53.  
  54.         boolean withCurrentElement = combinationSum(candidates, target - candidates[denominationIndex], denominationIndex, cache);
  55.         boolean withoutCurrentElement = combinationSum(candidates, target, denominationIndex + 1, cache);
  56.  
  57.         cache[target][denominationIndex] = withCurrentElement || withoutCurrentElement;
  58.  
  59.         return cache[target][denominationIndex];
  60.     }
  61.  
  62.     private static void generateSumOptions(int[] candidates, int target, int denominationIndex, Boolean[][] cache, List<List<Integer>> results, Stack<Integer> currentResult) {
  63.         if (target < 0) {
  64.             return;
  65.         }
  66.  
  67.         if (target == 0) {
  68.             List<Integer> currentResultClone = new ArrayList<>(currentResult);
  69.             results.add(currentResultClone);
  70.             return;
  71.         }
  72.  
  73.         if (denominationIndex == candidates.length) {
  74.             return;
  75.         }
  76.  
  77.         if (!cache[target][denominationIndex]) {
  78.             return;
  79.         }
  80.  
  81.         currentResult.push(candidates[denominationIndex]);
  82.         generateSumOptions(candidates, target - candidates[denominationIndex], denominationIndex, cache, results, currentResult);
  83.         currentResult.pop();
  84.  
  85.         generateSumOptions(candidates, target, denominationIndex + 1, cache, results, currentResult);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement