回答編集履歴

3

Pair コンストラクタ/メソッド名修正

2023/01/26 16:13

投稿

jimbe
jimbe

スコア12646

test CHANGED
@@ -7,11 +7,12 @@
7
7
  private static class Pair {
8
8
  final int a, b;
9
9
  Pair(int a, int b) {
10
+ if(a == b) throw new IllegalArgumentException("a == b");
10
- this.a = a;
11
+ this.a = Math.min(a, b);
11
- this.b = b;
12
+ this.b = Math.max(a, b);
12
13
  }
13
- boolean isRest(List<Integer> restList) {
14
+ boolean includedIn(List<Integer> list) {
14
- return restList.contains(a) || restList.contains(b);
15
+ return list.contains(a) || list.contains(b);
15
16
  }
16
17
  @Override
17
18
  public String toString() {
@@ -49,7 +50,7 @@
49
50
  //休みの人以外で、試合回数が少ないペアのリストを作成
50
51
  for(Map.Entry<Pair,Integer> entry : map.entrySet()) {
51
52
  Pair pair = entry.getKey();
52
- if(!pair.isRest(rest) && entry.getValue() == mingames) {
53
+ if(!pair.includedIn(rest) && entry.getValue() == mingames) {
53
54
  pairs.add(pair);
54
55
  }
55
56
  }

2

Pair に hashcode と equals 追加等

2023/01/26 07:23

投稿

jimbe
jimbe

スコア12646

test CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  ```java
4
4
  import java.util.*;
5
- import java.util.stream.Collectors;
6
5
 
7
6
  class StreamFilterTest {
8
7
  private static class Pair {
@@ -18,31 +17,51 @@
18
17
  public String toString() {
19
18
  return "["+a+", "+b+"]";
20
19
  }
20
+ @Override
21
+ public int hashCode() {
22
+ return Objects.hash(a, b);
23
+ }
24
+ @Override
25
+ public boolean equals(Object obj) {
26
+ if(this == obj) return true;
27
+ if(obj == null) return false;
28
+ if(getClass() != obj.getClass()) return false;
29
+ Pair other = (Pair)obj;
30
+ return a == other.a && b == other.b;
31
+ }
21
32
  }
22
33
 
23
34
  //ペアの組み合わせ作成用
24
- private static List<Pair> combination(int m) {
35
+ private static Map<Pair,Integer> combinationMap(int m, int initialValue) {
25
- List<Pair> comb = new ArrayList<>();
36
+ Map<Pair,Integer> map = new HashMap<>();
26
37
  for(int i=0; i<=m-2; i++) {
27
38
  for(int j=i+1; j<m; j++) {
28
- comb.add(new Pair(i, j));
39
+ map.put(new Pair(i, j), initialValue);
29
40
  }
30
41
  }
31
- return comb;
42
+ return map;
43
+ }
44
+
45
+ //休みの人以外で、試合回数が少ないペアのリストを作成
46
+ private static List<Pair> nextPairs(Map<Pair,Integer> map, List<Integer> rest, int mingames) {
47
+ //次に選択できるペアのリスト
48
+ List<Pair> pairs = new ArrayList<>();
49
+ //休みの人以外で、試合回数が少ないペアのリストを作成
50
+ for(Map.Entry<Pair,Integer> entry : map.entrySet()) {
51
+ Pair pair = entry.getKey();
52
+ if(!pair.isRest(rest) && entry.getValue() == mingames) {
53
+ pairs.add(pair);
54
+ }
55
+ }
56
+ return pairs;
32
57
  }
33
58
 
34
59
  public static void main(String[] args) {
35
60
  //参加する人数
36
61
  int M = 6;
37
62
 
38
- //ペアの全組み合わせ
39
- List<Pair> pairList = combination(M);
40
-
41
- //ペアが何回試合したかを保存するMapを作成
63
+ //ペアが何回試合したかを保存するMapを作成(テスト用に回数を2に設定)
42
- Map<Pair, Integer> pairMap = new LinkedHashMap<>();
64
+ Map<Pair, Integer> pairMap = combinationMap(M, 2);
43
-
44
- //テスト用に回数を2に設定
45
- pairMap = pairList.stream().collect(Collectors.toMap(l -> l, l -> 2));
46
65
  //テスト用に3ペアの回数を0に設定
47
66
  pairMap.put(new Pair(0, 1), 0);
48
67
  pairMap.put(new Pair(0, 2), 0);
@@ -58,16 +77,7 @@
58
77
  List<Integer> rest = Arrays.asList(1, 4);
59
78
  //List<Integer> rest = new ArrayList<>(Arrays.asList(1, 3, 4));
60
79
 
61
- //次に選択できるペアのリスト
62
- List<Pair> pairs = new ArrayList<>();
80
+ List<Pair> pairs = nextPairs(pairMap, rest, mingames);
63
-
64
- //休みの人以外で、試合回数が少ないペアのリストを作成
65
- for(Map.Entry<Pair,Integer> entry : pairMap.entrySet()) {
66
- Pair pair = entry.getKey();
67
- if(!pair.isRest(rest) && entry.getValue() == mingames) {
68
- pairs.add(pair);
69
- }
70
- }
71
81
 
72
82
  System.out.println("rest " + rest + " min " + mingames + " pairs" + pairs);
73
83
  }

1

結果追加、修正

2023/01/26 07:00

投稿

jimbe
jimbe

スコア12646

test CHANGED
@@ -5,7 +5,7 @@
5
5
  import java.util.stream.Collectors;
6
6
 
7
7
  class StreamFilterTest {
8
- static class Pair {
8
+ private static class Pair {
9
9
  final int a, b;
10
10
  Pair(int a, int b) {
11
11
  this.a = a;
@@ -21,7 +21,7 @@
21
21
  }
22
22
 
23
23
  //ペアの組み合わせ作成用
24
- static List<Pair> combination(int m) {
24
+ private static List<Pair> combination(int m) {
25
25
  List<Pair> comb = new ArrayList<>();
26
26
  for(int i=0; i<=m-2; i++) {
27
27
  for(int j=i+1; j<m; j++) {
@@ -73,3 +73,8 @@
73
73
  }
74
74
  }
75
75
  ```
76
+ 実行結果
77
+ ```
78
+ pairMap {[1, 4]=2, [0, 5]=2, [1, 5]=2, [2, 5]=2, [0, 4]=2, [3, 4]=2, [0, 3]=2, [4, 5]=2, [0, 2]=0, [0, 1]=0, [1, 3]=2, [1, 2]=2, [2, 3]=2, [2, 4]=2, [0, 3]=0, [0, 1]=2, [0, 2]=2, [3, 5]=2}
79
+ rest [1, 4] min 0 pairs[[0, 2], [0, 3]]
80
+ ```