VMware Coding Challenge: Possible Scores
Combination Sum I 那道题的变体
5 static int is_score_possible(int score, int[] increments) { 6 Arrays.sort(increments); 7 ArrayList<Integer> res = new ArrayList<Integer>(); 8 res.add(0); 9 helper(res, increments, score, 0); 10 return res.get(0); 11 } 12 13 public static void helper(ArrayList<Integer> res, int[] increments, int score, int start) { 14 if (score < 0) return; 15 if (score == 0) { 16 res.set(0, 1); 17 return; 18 } 19 for (int i=start; i<increments.length; i++) { 20 if (i>start && increments[i] == increments[i-1]) continue; 21 helper(res, increments, score-increments[i], i); 22 if (res.get(0) == 1) return; 23 } 24 }