# Nhut Nguyen # Problem statement

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

Return the minimum number of rotations so that all the values in `tops` are the same, or all the values in `bottoms` are the same.

If it cannot be done, return `-1`.

## 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 * 10^4`.
• `bottoms.length == tops.length`.
• `1 <= tops[i], bottoms[i] <= 6`.

# Solution: Count and Compare

"All the values in `tops` are the same" means they are equal to `tops`.

Similarly, "all the values in `botoms` are the same" means they are equal to `bottoms`.

You just need to compute the result for `tops` and for `bottoms` and compare them for the final result.

## Code

``````#include <vector>
#include <iostream>
using namespace std;
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int topCount = 1;
int bottomCount = 0;
for (int i = 1; i < tops.size(); i++) {
if (tops[i] != tops && bottoms[i] != tops) {
tops = 0;
break;
}
topCount += tops[i] == tops;
bottomCount += bottoms[i] == tops;
}
int topResult = tops ? tops.size() - max(topCount, bottomCount) : -1;
if (topResult == 0) {
return 0;
}
bottomCount = 1;
topCount = 0;
for (int i = 1; i < bottoms.size(); i++) {
if (tops[i] != bottoms && bottoms[i] != bottoms) {
bottoms = 0;
break;
}
topCount += tops[i] == bottoms;
bottomCount += bottoms[i] == bottoms;
}
int bottomResult = bottoms ? bottoms.size() - max(topCount, bottomCount) : -1;
return  (topResult >= 0 && bottomResult >= 0) ?
min(topResult, bottomResult) : max(topResult, bottomResult);
}
int main() {
vector<int> tops{2,1,2,4,2,2};
vector<int> bottoms{5,2,6,2,3,2};
cout << minDominoRotations(tops, bottoms) << endl;
tops = {1,2,1,1,1,2,2,2};
bottoms = {2,1,2,2,2,2,2,2};
cout << minDominoRotations(tops, bottoms) << endl;
}
``````
``````Output:
2
1
``````

## Complexity

• Runtime: `O(N)`, where `N = tops.length`.
• Extra space: `O(1)`.

# Implementation notes

• To save one or two boolean variables for flagging whether the swapping happens, you can use/modify the value of the fixed values that you choose to compare with the others. Here they are `tops` and `bottoms`.
• The value of a boolean expression, e.g. `tops[i] == tops` is casted to `0` (`false`) or `1` (`true`) when it is used in an `int` expression. So you can write `topCount += tops[i] == tops`.