Minimum Domino Rotations For Equal Row (LeetCode 1007): Solution and Explanation

Medium
Array
Greedy

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

  1. Take the candidates tops[0] and bottoms[0].
  2. For a candidate x, set rot_top = rot_bottom = 0 and walk every domino.
  3. If neither half of a domino is x, stop: this candidate cannot work.
  4. Otherwise add 1 to rot_top when the top is not x, and add 1 to rot_bottom when the bottom is not x.
  5. 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]
  1. The candidates are tops[0] = 2 and bottoms[0] = 5. Try x = 2.
  2. Domino 0 is 2|5: the top is 2, so only the bottom row would need a rotation. rot_bottom = 1.
  3. Domino 1 is 1|2: the 2 is on the bottom, so the top row needs a rotation. rot_top = 1.
  4. 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.
  5. Domino 5 is 2|2 and costs nothing. Every domino had a 2.
  6. 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 -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each candidate needs at most one pass over the dominoes, and there are only two candidates.

Space

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 trying bottoms[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.