1007. Minimum Domino Rotations For Equal Row
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public int minDominoRotations(int[] A, int[] B) {
if(A == null || B == null) return 0;
int minRotation = Math.min(flip(A, B, A[0]), flip(B, A, B[0]));
return (minRotation == Integer.MAX_VALUE)? -1: minRotation;
}
private int flip(int[] M, int[] N, int target) {
int mSwap = 0, nSwap = 0;
for(int i = 0; i < M.length; i++) {
if(M[i] != target && N[i] != target) {
return Integer.MAX_VALUE;
}
if(M[i] != target) mSwap++;
if(N[i] != target) nSwap++;
}
return Math.min(mSwap, nSwap);
}
}