-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1007.java
More file actions
35 lines (27 loc) · 1.09 KB
/
Copy pathSolution1007.java
File metadata and controls
35 lines (27 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.HashSet;
import java.util.Set;
public class Solution1007 {
public int minDominoRotations(int[] tops, int[] bottoms) {
int[] variants = new int[]{tops[0], bottoms[0]};
for (int i = 1; i < tops.length; i++) {
for (int v = 0; v < variants.length; v++) {
int variant = variants[v];
if (variant != tops[i] && variant != bottoms[i]) variants[v] = 0;
}
if (variants[0] == 0 && variants[1] == 0) return -1;
}
int countTops = 0;
int countBottoms = 0;
int variant = variants[0] == 0 ? variants[1] : variants[0];
for (int i = 0; i < tops.length; i++) {
if (tops[i] == variant) countTops++;
if (bottoms[i] == variant) countBottoms++;
}
return tops.length - Math.max(countTops, countBottoms);
}
public static void main(String[] args) {
int[] tops = new int[]{1,2,1,1,1,2,2,2};
int[] bottoms = new int[]{2,1,2,2,2,2,2,2};
System.out.println(new Solution1007().minDominoRotations(tops, bottoms));
}
}