Minimum Domino Rotations For Equal Row

Updated on 12 June, 2025
Minimum Domino Rotations For Equal Row header image

Problem Statement

In this scenario, we deal with a series of domino tiles arranged in a row, each characterized by two values: tops[i] and bottoms[i] for the i-th domino. The values range between 1 and 6, symbolizing the face values of each half of the domino tile. You are allowed to rotate any domino to switch its top and bottom values. The challenge involves finding the minimum number of such rotations needed so that all values align uniformly on either the top or the bottom side across all dominoes. Your objective is to return this minimal rotation count. If aligning all dominos uniformly is impossible under the given configuration, your solution should indicate this by returning -1.

Examples

Example 1

Input:

tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]

Output:

2

Explanation:

The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.

Example 2

Input:

tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]

Output:

-1

Explanation:

In this case, it is not possible to rotate the dominoes to make one row of values equal.

Constraints

  • 2 <= tops.length <= 2 * 104
  • bottoms.length == tops.length
  • 1 <= tops[i], bottoms[i] <= 6

Approach and Intuition

  1. First, understand that for uniform values across all the tops or all the bottoms, one potential strategy is focusing on making either just one value (from either tops or bottoms) consistent across all the array. This leads to the idea of trying out each value (from 1 to 6 which is the range of domino values) and checking how feasible it is.
  2. Recognize two major scenarios for each of the target values (from 1 to 6):
    • Count how many rotations are needed to make all tops the chosen value.
    • Count how many rotations are needed to make all bottoms the chosen value.
  3. Implement this check by iterating through both tops and bottoms arrays simultaneously:
    • If the target value matches the top value, no rotation is needed for that particular domino for aligning tops.
    • If the target value matches the bottom value, no rotation is necessary for that particular domino for aligning bottoms.
    • If a domino needs a switch to meet the target value, increment the corresponding rotation count.
  4. While iterating, if you find at least one domino where neither the top nor bottom can match the target value after rotation, this implies it is impossible to align all to this target value.
  5. Repeat this check for each value from 1 to 6, tracking the minimal rotations needed which successfully align all dominoes.
  6. If no value from 1 to 6 allows perfect alignment, the answer should be -1.
  7. This approach relies heavily on the concept of trying potential solutions and immediately discarding unfeasible ones, essentially optimizing through direct inspection and elimination.

The logic described aims to effectively navigate through possible configurations with a factor of computational feasibility, balancing between brute force inspection and immediate pruning of unachievable states. Using this basis, the solution can be both understood and implemented straightforwardly.

Solutions

  • C++
  • Java
cpp
class Solution {
  public:
    int min_rotations(int target, vector<int>& tops, vector<int>& bottoms, int length) {
        int rotate_tops = 0, rotate_bottoms = 0;
        for (int index = 0; index < length; index++) {
            if (tops[index] != target && bottoms[index] != target) return -1;
            if (tops[index] != target) rotate_tops++;
            if (bottoms[index] != target) rotate_bottoms++;
        }
        return min(rotate_tops, rotate_bottoms);
    }

    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
        int length = tops.size();
        int result = min_rotations(tops[0], tops, bottoms, length);
        if (result != -1 || tops[0] == bottoms[0]) return result;
        return min_rotations(bottoms[0], tops, bottoms, length);
    }
};

In the given C++ solution, the goal is to find the minimum number of rotations required to make all the values in either the top or bottom row of dominos the same. This is achieved through the following approach:

  • Implement two helper functions within the Solution class:

    • min_rotations(int target, vector<int>& tops, vector<int>& bottoms, int length)

      • This function calculates the minimum rotations needed to make all dominos show the target value on either top or bottom.
      • It iterates through each domino, incrementing counters for required rotations when the current top or bottom doesn't match the target value.
      • If a domino is encountered where both the top and bottom cannot be the target, the function returns -1, indicating it's impossible for that target value.
      • The minimal number of rotations from either the top or bottom is returned using the min function.
    • minDominoRotations(vector<int>& tops, vector<int>& bottoms)

      • This is the primary function which tries to align all dominos to show either the first top or first bottom value across all dominos.
      • It first calls min_rotations using the first element of tops as the target.
      • If making all tops or bottoms the same as tops[0] is possible, it returns the result.
      • Otherwise, it repeats using bottoms[0] as the target value.
      • This dual attempt ensures that if either the first top or bottom value can propagate through the entire row successfully with minimal rotations, the correct minimal count is obtained.

Overall, this algorithm systematically checks feasibility for minimal rotations with a two-target approach, efficiently dealing with the problem constraints using loops and conditional checks.

java
class Solution {
    public int minRotations(int x, int[] top, int[] bottom, int count) {
        int rotateTop = 0, rotateBottom = 0;
        for (int i = 0; i < count; i++) {
            if (top[i] != x && bottom[i] != x) return -1;
            if (top[i] != x) rotateBottom++;
            if (bottom[i] != x) rotateTop++;
        }
        return Math.min(rotateTop, rotateBottom);
    }

    public int minimumDominoRotations(int[] top, int[] bottom) {
        int length = top.length;
        int rotations = minRotations(top[0], top, bottom, length);
        if (rotations != -1 || top[0] == bottom[0]) return rotations;
        else return minRotations(bottom[0], top, bottom, length);
    }
}

The provided Java code defines a solution to find the minimum number of rotations needed to make all the dominoes in either the top row or bottom row identical. The solution is implemented within a class named Solution that contains two primary methods:

  • minRotations(int x, int[] top, int[] bottom, int count) - This method calculates the minimum number of rotations required to make all the values in either the top or bottom array equal to x. It takes x (the target value for either row), arrays top and bottom (representing the current values of dominoes in each row), and count (the length of the arrays which is the number of domino pieces). The method initializes two counters: rotateTop and rotateBottom to track the number of flips needed for the top and bottom rows, respectively. As it iterates through each domino, it checks whether a rotation is needed. If neither domino at position i matches x, it immediately returns -1, indicating it's impossible to make one entire row equal to x. Otherwise, it updates rotation counters accordingly. The method returns the minimum of the two counters.

  • minimumDominoRotations(int[] top, int[] bottom) - This method is used to determine the minimum rotations needed by trying to align all domino tops or all domino bottoms to be the same. It begins by trying to make the rows uniform using the first domino's top value. If this fails and the values of the domino on the top and bottom at the first position aren't the same, it then tries using the first domino's bottom value. The function outputs the minimum rotations needed or -1 if it's not possible.

The solution leverages direct computation and conditional logic to efficiently solve the problem with a focus on minimal computation and immediate termination when it's determined that a solution is not feasible.

Comments

No comments yet.