Formatted question description: https://leetcode.ca/all/1007.html

1007. Minimum Domino Rotations For Equal Row

Level

Medium

Description

In a row of dominoes, A[i] and B[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 A[i] and B[i] swap values.

Return the minimum number of rotations so that all the values in A are the same, or all the values in B are the same.

If it cannot be done, return -1.

Example 1:

Image text

Input: A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]

Output: 2

Explanation:

The first figure represents the dominoes as given by A and B: 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: A = [3,5,1,2,3], B = [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.

Note:

  1. 1 <= A[i], B[i] <= 6
  2. 2 <= A.length == B.length <= 20000

Solution

For each possible number from 1 to 6, store the indices of the number in A and B.

A number can be in all dominoes after rotations only if a number exists at every index in either A or B or both arrays. Count the numbers of occurrences of the number in A and B, and calculate the minimum number of rotations required, and return.

class Solution {
    public int minDominoRotations(int[] A, int[] B) {
        Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
        int length = A.length;
        for (int i = 0; i < length; i++) {
            int numA = A[i];
            List<Integer> indicesA = map.getOrDefault(numA, new ArrayList<Integer>());
            indicesA.add(i + 1);
            map.put(numA, indicesA);
            int numB = B[i];
            List<Integer> indicesB = map.getOrDefault(numB, new ArrayList<Integer>());
            indicesB.add(-i - 1);
            map.put(numB, indicesB);
        }
        int minRotation = Integer.MAX_VALUE;
        Set<Integer> keySet = map.keySet();
        for (int key : keySet) {
            List<Integer> indices = map.getOrDefault(key, new ArrayList<Integer>());
            Set<Integer> indicesSet = new HashSet<Integer>();
            for (int index : indices)
                indicesSet.add(Math.abs(index));
            if (indicesSet.size() == length) {
                Collections.sort(indices, new Comparator<Integer>() {
                    public int compare(Integer num1, Integer num2) {
                        return Math.abs(num1) - Math.abs(num2);
                    }
                });
                int countA = 0, countB = 0;
                int size = indices.size();
                for (int i = 0; i < size; i++) {
                    int index = indices.get(i);
                    if (index > 0)
                        countA++;
                    else
                        countB++;
                }
                int curMinRotation = length - Math.max(countA, countB);
                minRotation = Math.min(minRotation, curMinRotation);
            }
        }
        return minRotation == Integer.MAX_VALUE ? -1 : minRotation;
    }
}

All Problems

All Solutions