class Solution:
def minDominoRotations(self, tops, bottoms):
def check(x):
rotations_top = rotations_bottom = 0
for i in range(len(tops)):
if tops[i] != x and bottoms[i] != x:
return -1
elif tops[i] != x:
rotations_top += 1
elif bottoms[i] != x:
rotations_bottom += 1
return min(rotations_top, rotations_bottom)
rotations = check(tops[0])
if rotations != -1:
return rotations
else:
return check(bottoms[0])
class Solution {
public:
int check(int x, vector<int>& tops, vector<int>& bottoms) {
int rotations_top = 0, rotations_bottom = 0;
for (int i = 0; i < tops.size(); ++i) {
if (tops[i] != x && bottoms[i] != x) return -1;
else if (tops[i] != x) rotations_top++;
else if (bottoms[i] != x) rotations_bottom++;
}
return min(rotations_top, rotations_bottom);
}
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int rotations = check(tops[0], tops, bottoms);
if (rotations != -1) return rotations;
else return check(bottoms[0], tops, bottoms);
}
};
class Solution {
public int minDominoRotations(int[] tops, int[] bottoms) {
int rotations = check(tops[0], tops, bottoms);
if (rotations != -1) return rotations;
else return check(bottoms[0], tops, bottoms);
}
private int check(int x, int[] tops, int[] bottoms) {
int rotationsTop = 0, rotationsBottom = 0;
for (int i = 0; i < tops.length; ++i) {
if (tops[i] != x && bottoms[i] != x) return -1;
else if (tops[i] != x) rotationsTop++;
else if (bottoms[i] != x) rotationsBottom++;
}
return Math.min(rotationsTop, rotationsBottom);
}
}
var minDominoRotations = function(tops, bottoms) {
function check(x) {
let rotationsTop = 0, rotationsBottom = 0;
for (let i = 0; i < tops.length; i++) {
if (tops[i] !== x && bottoms[i] !== x) return -1;
else if (tops[i] !== x) rotationsTop++;
else if (bottoms[i] !== x) rotationsBottom++;
}
return Math.min(rotationsTop, rotationsBottom);
}
let rotations = check(tops[0]);
if (rotations !== -1) return rotations;
return check(bottoms[0]);
};
You are given two integer arrays, tops
and bottoms
, each of length n
. These arrays represent the numbers on the top and bottom halves of n
dominoes, respectively. You can rotate any domino, swapping its top and bottom values.
The goal is to determine the minimum number of rotations needed so that all values in either the tops
array or the bottoms
array are the same. If it is not possible to achieve this, return -1
.
Constraints:
At first glance, it seems we might need to try all possible numbers (1 through 6) and check if we can make all dominoes show that number on either the top or bottom. A brute-force approach would check every possible value and count the minimum rotations required.
However, upon closer inspection, we realize that only the numbers present in the first domino (either tops[0]
or bottoms[0]
) can possibly be the unified value. This is because every domino must have the target number on at least one of its sides. If any domino lacks the candidate number on both sides, it's impossible to achieve uniformity with that number.
This insight shifts us from checking all numbers (brute-force) to checking at most two candidates, greatly reducing unnecessary computation.
tops[0]
and bottoms[0]
. If it's possible to make all dominoes show one of these values on either side, then that is our answer.x
):
x
, it's impossible to use x
as the target—return -1
.x
but the bottom is, we would need to rotate this domino so x
comes to the top.x
but the top is, we would need to rotate this domino so x
comes to the bottom.tops[0]
and bottoms[0]
as candidates. If both fail, return -1
.
Consider tops = [2,1,2,4,2,2]
and bottoms = [5,2,6,2,3,2]
.
tops
or bottoms
equal to tops[0] = 2
:
Rotations needed to make all tops 2: 2 (domino 1 and 3). Rotations needed to make all bottoms 2: 1 (domino 0).
bottoms[0] = 5
:
So, the answer is 2 (minimum rotations to make all tops or all bottoms 2).
tops[0]
and bottoms[0]
), and for each, scan through the dominoes once.
The key insight is that only the numbers present in the first domino can possibly be made uniform across the entire row. By checking only these candidates and counting the minimal rotations needed for each, we achieve an efficient O(n) solution. This avoids unnecessary computation and demonstrates the power of narrowing the problem space based on problem constraints.
The solution is elegant because it leverages a simple observation to reduce complexity, uses clear logic, and is easy to implement in any mainstream programming language.