Minimum Domino Rotations For Equal Row - Problem
In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the i-th domino. A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.
We may rotate the i-th 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.
If it cannot be done, return -1.
Input & Output
Example 1 — Basic Case
$
Input:
tops = [2,1,2,1], bottoms = [1,2,1,2]
›
Output:
2
💡 Note:
We can make all tops equal to 2 by rotating dominoes at indices 1 and 3. After rotation: tops = [2,2,2,2]. Total rotations needed: 2.
Example 2 — Impossible Case
$
Input:
tops = [3,5,1,2], bottoms = [3,6,3,3]
›
Output:
-1
💡 Note:
No single value appears in every domino position (tops[i] or bottoms[i]), so it's impossible to make all tops or all bottoms uniform.
Example 3 — No Rotations Needed
$
Input:
tops = [1,1,1], bottoms = [2,2,2]
›
Output:
0
💡 Note:
The tops are already all equal to 1, so no rotations are needed. Answer is 0.
Constraints
- 2 ≤ tops.length == bottoms.length ≤ 2 × 104
- 1 ≤ tops[i], bottoms[i] ≤ 6
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code