This is a place to put my clutters, no matter you like it or not, welcome here.
0%
1007. Minimum Domino Rotations For Equal Row
Posted onEdited onIn面試Views: Symbols count in article: 1.3kReading time ≈1 mins.
In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
We may rotate the ith domino, so that tops[i] and bottoms[i] swap values.
Return the minimum number of rotations so that all the values in tops are the same, or all the values in bottoms are the same.
TS: O(n), SC: O(n) class Solution { public int minDominoRotations(int[] top, int[] bottom) { int n = top.length; int a = top[0], b = bottom[0]; // rotate from [0] for(int i = 1 ; i < n ; i++) { // mark number different from [0] to -1 if(a != top[i] && a != bottom[i]) a = -1; if(b != top[i] && b != bottom[i]) b = -1; // different from two head, -1 if(a == -1 && b == -1) return -1; } if(a == -1) a=b; // count swap number int tswap = 0, bswap = 0; for(int i = 0 ; i < n ; i++) { if(a != top[i]) tswap++; if(a != bottom[i]) bswap++; } return Math.min(tswap, bswap); } }