Minimum Domino Rotations For Equal Row LeetCode solution in Java

1
class Solution {
2
public int minDominoRotations(int[] tops, int[] bottoms) {
3

4
int[][] c = new int[6][2];
5

6
for (int i : tops) {
7
c[i - 1][0]++;
8
}
9
for (int i : bottoms) {
10
c[i - 1][1]++;
11
}
12
int[] common = new int[6];
13
for (int i = 0; i < tops.length; i++) {
14
if (tops[i] == bottoms[i]) {
15
common[tops[i] - 1]++;
16
}
17
}
18
int min = Integer.MAX_VALUE;
19
for (int i = 1; i <= 6; i++) {
20
if (c[i - 1][0] + c[i - 1][1] >= tops.length) {
21
if (c[i - 1][0] >= c[i - 1][1]
22
&& c[i - 1][1] - common[i - 1] + c[i - 1][0] == tops.length) {
23
min = Math.min(min, c[i - 1][1] - common[i - 1]);
24
} else if (c[i - 1][1] >= c[i - 1][0]
25
&& c[i - 1][0] - common[i - 1] + c[i - 1][1] == tops.length) {
26
int left = c[i - 1][0] - common[i - 1];
27
min = Math.min(min, c[i - 1][0] - common[i - 1]);
28
}
29
}
30
}
31

32
return min == Integer.MAX_VALUE ? -1 : min;
33
}
34
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0