重複ありのカウント
Java
1public static int findListCount_(final int score, final int[] v) {
2 int[] result = {0};
3 Deque<Integer> vlist = IntStream.of(v).mapToObj(Integer::valueOf).sorted().collect(Collectors.toCollection(ArrayDeque::new));
4 go_(score, vlist, 0, result);
5 return result[0];
6}
7
8public static void go_(final int score, final Deque<Integer> rem, final int sum, final int[] acc) {
9 //System.out.println(rem +" - " + candidate + " - " + acc[0]);
10 if (sum >= score) {
11 acc[0]++;
12 return;
13 }
14 while (!rem.isEmpty()) {
15 int val = rem.removeLast();
16 if (val >= score) {
17 acc[0]++;
18 continue;
19 }
20 go_(score, new ArrayDeque<>(rem), sum+val, acc);
21 }
22}
テストコード
Java
1import java.util.ArrayDeque;
2import java.util.ArrayList;
3import java.util.Arrays;
4import java.util.Collection;
5import java.util.Deque;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Set;
9import java.util.stream.Collectors;
10import java.util.stream.IntStream;
11
12public class MinSet_ {
13
14 public static void main(String... args) {
15 int[] v = { 2, 3, 5, 1, 8, 3 };
16 System.out.println("v = " + Arrays.toString(v));
17 for (int score= 1; score <= 23; score++) {
18 //System.out.println("score " + score);
19 System.out.println("score " + score + " : " + findList(score, v) + " : " + findListCount_(score, v));
20 }
21 //
22 System.out.println("--------------------------------------");
23 for (int score= 1; score <= 23; score++) {
24 //System.out.println("score " + score);
25 Collection<List<Integer>> result = findSet(score, v);
26 System.out.println("score " + score + " : " + result + " : " + result.size());
27 }
28 }
29
30 public static Collection<List<Integer>> findList(final int score, final int[] v) {
31 List<List<Integer>> result = new ArrayList<>();
32 Deque<Integer> vlist = IntStream.of(v).mapToObj(Integer::valueOf).sorted().collect(Collectors.toCollection(ArrayDeque::new));
33 go(score, vlist, new ArrayList<>(), result);
34 return result;
35 }
36
37 public static Collection<List<Integer>> findSet(final int score, final int[] v) {
38 Set<List<Integer>> result = new HashSet<>();
39 Deque<Integer> vlist = IntStream.of(v).mapToObj(Integer::valueOf).sorted().collect(Collectors.toCollection(ArrayDeque::new));
40 go(score, vlist, new ArrayList<>(), result);
41 return result;
42 }
43
44 public static void go(final int score, final Deque<Integer> rem, final List<Integer> candidate, final Collection<List<Integer>> acc) {
45 //System.out.println(rem +" - " + candidate + " - " + acc);
46 long sum = candidate.stream().collect(Collectors.summingLong(Long::valueOf));
47 if (sum >= score) {
48 acc.add(candidate);
49 return;
50 }
51 while (!rem.isEmpty()) {
52 List<Integer> uu = new ArrayList<>(candidate);
53 int val = rem.removeLast();
54 uu.add(val);
55 if (val >= score) {
56 acc.add(uu);
57 continue;
58 }
59 go(score, new ArrayDeque<>(rem), uu, acc);
60 }
61 }
62
63 public static int findListCount_(final int score, final int[] v) {
64 int[] result = {0};
65 Deque<Integer> vlist = IntStream.of(v).mapToObj(Integer::valueOf).sorted().collect(Collectors.toCollection(ArrayDeque::new));
66 go_(score, vlist, 0, result);
67 return result[0];
68 }
69
70 public static void go_(final int score, final Deque<Integer> rem, final int sum, final int[] acc) {
71 //System.out.println(rem +" - " + candidate + " - " + acc[0]);
72 if (sum >= score) {
73 acc[0]++;
74 return;
75 }
76 while (!rem.isEmpty()) {
77 int val = rem.removeLast();
78 if (val >= score) {
79 acc[0]++;
80 continue;
81 }
82 go_(score, new ArrayDeque<>(rem), sum+val, acc);
83 }
84 }
85
86}