Skip to content

1007. Minimum Domino Rotations For Equal Row 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
 public:
  int minDominoRotations(vector<int>& A, vector<int>& B) {
    const int n = A.size();
    vector<int> countA(7);
    vector<int> countB(7);
    vector<int> countBoth(7);

    for (int i = 0; i < n; ++i) {
      ++countA[A[i]];
      ++countB[B[i]];
      if (A[i] == B[i])
        ++countBoth[A[i]];
    }

    for (int i = 1; i <= 6; ++i)
      if (countA[i] + countB[i] - countBoth[i] == n)
        return n - max(countA[i], countB[i]);

    return -1;
  }
};
1
2
3
4
5
6
class Solution:
  def minDominoRotations(self, A: List[int], B: List[int]) -> int:
    for num in range(1, 7):
      if all(num in pair for pair in zip(A, B)):
        return len(A) - max(A.count(num), B.count(num))
    return -1