-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1007. Minimum Domino Rotations For Equal Row.cpp
111 lines (72 loc) · 2.68 KB
/
1007. Minimum Domino Rotations For Equal Row.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino.
(A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
We may rotate the ith 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 * 104
bottoms.length == tops.length
1 <= tops[i], bottoms[i] <= 6
/* solution 1 */
class Solution {
public:
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int res = INT_MAX, n = tops.size();
for (int i = 1; i <= 6; i++) {
int rotate = 0;
bool flag = true;
for (int j = 0; j < n; j++) {
if (tops[j] == i) continue;
else if (bottoms[j] == i) rotate++;
else {
flag = false; break;
}
}
if (flag) res = min(res, rotate);
}
for (int i = 1; i <= 6; i++) {
int rotate = 0;
bool flag = true;
for (int j = 0; j < n; j++) {
if (bottoms[j] == i) continue;
else if (tops[j] == i) rotate++;
else {
flag = false; break;
}
}
if (flag) res = min(res, rotate);
}
if (res == INT_MAX) res = -1;
return res;
}
};
/* soution 2 */
class Solution {
public:
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int res = INT_MAX, n = tops.size();
vector <int> counta(7, 0), countb(7, 0), same(7, 0);
for (int i = 0; i < n; i++) {
counta[tops[i]]++;
countb[bottoms[i]]++;
if (tops[i] == bottoms[i]) same[tops[i]]++;
}
for (int i = 1; i < 7; i++) {
if ((counta[i] + countb[i] - same[i]) == n)
res = min(res, n - max(counta[i], countb[i]));
}
if (res == INT_MAX) res = -1;
return res;
}
};