Minimum Domino Rotations For Equal Row (LeetCode 1007): Solution and Explanation
Each domino has a top and a bottom value from 1 to 6. A rotation swaps the two halves of one domino. Find the fewest rotations that make the whole top row equal, or the whole bottom row equal, or return -1 if neither is possible.
Intuition
Start from the end state. If the top row is all x after some rotations, then every domino had an x on one of its halves to begin with, because a rotation only moves values between the halves of the same domino. The same holds for the bottom row.
That rules out almost every value. Whatever x is, domino 0 must carry it, so x is either tops[0] or bottoms[0]. Instead of trying all six faces, you only need to test two candidates.
Testing a candidate is a single pass. If some domino has x on neither half, the candidate is dead. Otherwise every top that is not x needs one rotation to fix the top row, and every bottom that is not x needs one rotation to fix the bottom row. A domino with x on both halves costs nothing either way.
If the first candidate works, its answer is already the best one. When both candidates survive, every domino holds both values, and the two candidates give the same minimum by symmetry, so there is no need to compare them.
Approach: Two candidates, one scan each
- Take the candidates
tops[0]andbottoms[0]. - For a candidate
x, setrot_top = rot_bottom = 0and walk every domino. - If neither half of a domino is
x, stop: this candidate cannot work. - Otherwise add 1 to
rot_topwhen the top is notx, and add 1 torot_bottomwhen the bottom is notx. - If the scan finishes, return
min(rot_top, rot_bottom). If both candidates fail, return -1.
Step-by-step example
tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
- The candidates are tops[0] = 2 and bottoms[0] = 5. Try x = 2.
- Domino 0 is 2|5: the top is 2, so only the bottom row would need a rotation. rot_bottom = 1.
- Domino 1 is 1|2: the 2 is on the bottom, so the top row needs a rotation. rot_top = 1.
- Domino 2 is 2|6, so rot_bottom = 2. Domino 3 is 4|2, so rot_top = 2. Domino 4 is 2|3, so rot_bottom = 3.
- Domino 5 is 2|2 and costs nothing. Every domino had a 2.
- Making the top row all 2 costs 2 rotations and the bottom row costs 3, so the answer is 2.
Code: Python, Java, C++
1class Solution:
2 def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
3 for x in (tops[0], bottoms[0]):
4 rot_top = rot_bottom = 0
5 ok = True
6 for t, b in zip(tops, bottoms):
7 if t != x and b != x:
8 ok = False
9 break
10 if t != x:
11 rot_top += 1
12 if b != x:
13 rot_bottom += 1
14 if ok:
15 return min(rot_top, rot_bottom)
16 return -11class Solution {
2 public int minDominoRotations(int[] tops, int[] bottoms) {
3 int[] candidates = {tops[0], bottoms[0]};
4 for (int x : candidates) {
5 int rotTop = 0, rotBottom = 0;
6 boolean ok = true;
7 for (int i = 0; i < tops.length; i++) {
8 if (tops[i] != x && bottoms[i] != x) {
9 ok = false;
10 break;
11 }
12 if (tops[i] != x) rotTop++;
13 if (bottoms[i] != x) rotBottom++;
14 }
15 if (ok) {
16 return Math.min(rotTop, rotBottom);
17 }
18 }
19 return -1;
20 }
21}1class Solution {
2public:
3 int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
4 int candidates[2] = {tops[0], bottoms[0]};
5 for (int x : candidates) {
6 int rotTop = 0, rotBottom = 0;
7 bool ok = true;
8 for (int i = 0; i < (int)tops.size(); i++) {
9 if (tops[i] != x && bottoms[i] != x) {
10 ok = false;
11 break;
12 }
13 if (tops[i] != x) rotTop++;
14 if (bottoms[i] != x) rotBottom++;
15 }
16 if (ok) {
17 return min(rotTop, rotBottom);
18 }
19 }
20 return -1;
21 }
22};Time and space complexity
O(n)
Each candidate needs at most one pass over the dominoes, and there are only two candidates.
O(1)
Only two counters and a flag are kept, regardless of how many dominoes there are.
Other approaches
Count faces for all six values (time O(n), space O(1))
Count how often each value appears on top, on bottom, and on both halves of the same domino. A value x works when topCount[x] + bottomCount[x] - sameCount[x] equals n, and the answer for it is n minus the larger of topCount[x] and bottomCount[x]. It is also linear and avoids the early exit logic.
Try every value from 1 to 6 (time O(n), space O(1))
Run the same scan as the main solution for all six faces and keep the best result. It is simpler to reason about but does up to three times more work than testing just the two candidates.
Common mistakes
- Counting a domino with x on both halves as a rotation for one of the rows. It already fits both rows and costs nothing.
- Only checking how to fill the top row. The bottom row is a separate target and can be cheaper.
- Returning -1 as soon as
tops[0]fails without tryingbottoms[0]. The second candidate can still succeed. - Counting rotations as the number of dominoes where the halves differ. Only the halves that are not x matter for each row.
Frequently asked questions
Why are tops[0] and bottoms[0] the only candidates?
Rotating a domino never brings in a new value, it only swaps its two halves. So a value that fills a whole row must already sit on every domino, and that includes domino 0.
If the first candidate works, could the second give a smaller answer?
No. If both candidates work, every domino holds both values, so the counts for one candidate are the mirror image of the other's and the minimum is the same.
What if tops[0] equals bottoms[0]?
Then there is really only one candidate. Trying it twice is harmless: if it fails the first time, it fails the second time too.
What is the time complexity?
O(n) time and O(1) extra space, since at most two passes are made over the dominoes.