Minimum Domino Rotations For Equal Row LeetCode solution in C++

1
class Solution {
2
public:
3
int minDominoRotations(vector<int> &tops, vector<int> &bottoms) {
4
vector<int> freq(7, 0);
5

6
int n = tops.size();
7
int tile = -1
8
// there has to be particular tile number which is present in
9
// every column to be able to arrange same tile in top or bottom
10
// by rotating check which tile is present in every column
11
for (int i = 0; i < n; ++i) {
12
int top = tops[i];
13
int bottom = bottoms[i];
14

15
freq[top]++;
16
if (top != bottom) {
17
freq[bottom]++;
18
}
19
// check for potential tile number
20
if (freq[top] == n) {
21
tile = top;
22
}
23
if (freq[bottom] == n) {
24
tile = bottom;
25
}
26
}
27

28
if (tile == -1) { // rearrangement not possible
29
return -1;
30
}
31

32
int tilesTop = 0;
33
int tilesBottom = 0;
34
for (int i = 0; i < n; ++i) {
35
if (tops[i] == bottoms[i]) continue;
36

37
if (tops[i] == tile) {
38
tilesTop++;
39
}
40
if (bottoms[i] == tile) {
41
tilesBottom++;
42
}
43
}
44

45
return tilesTop < tilesBottom ? tilesTop : tilesBottom;
46
}
47
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0