-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumDominoRotationsForEqualRow.java
More file actions
31 lines (29 loc) · 1.11 KB
/
MinimumDominoRotationsForEqualRow.java
File metadata and controls
31 lines (29 loc) · 1.11 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
import java.util.Arrays;
/*
* https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/
*/
public class MinimumDominoRotationsForEqualRow {
public int minDominoRotations(int[] A, int[] B) {
int length = A.length;
int minSoFar = Integer.MAX_VALUE;
outer:
for (int num = 1; num <= 6; num++) {
for (int i = 0; i < length; i++) {
if (A[i] != num && B[i] != num) {
continue outer;
}
}
int finalNum = num;
int topCount = (int) Arrays.stream(A).filter(value -> value == finalNum).count();
int bottomCount = (int) Arrays.stream(B).filter(value -> value == finalNum).count();
minSoFar = Math.min(length - Math.max(topCount, bottomCount), minSoFar);
}
return minSoFar == Integer.MAX_VALUE ? -1 : minSoFar;
}
public static void main(String[] args) {
System.out.println(new MinimumDominoRotationsForEqualRow().minDominoRotations(
new int[]{2, 1, 2, 4, 2, 2},
new int[]{5, 2, 6, 2, 3, 2}
)); // 2
}
}