-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1007-minimum-domino-rotations-for-equal-row.cpp
55 lines (47 loc) · 1.31 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
class Solution {
public:
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int n = tops.size();
int a = tops[0];
int cnt1 = 0, cnt2 = 0, temp = 0;
for(int i = 1; i < n; i++) {
if(tops[i] == a and bottoms[i] == a) {
temp++;
continue;
}
if(tops[i] == a) {
cnt1++;
}
if(bottoms[i] == a) {
cnt2++;
}
}
if(cnt1 + cnt2 + temp == n - 1) {
if(tops[0] == bottoms[0]) {
return min(cnt1, cnt2);
}
return min(cnt1 + 1, cnt2);
}
a = bottoms[0];
cnt1 = 0, cnt2 = 0, temp = 0;
for(int i = 1; i < n; i++) {
if(tops[i] == a and bottoms[i] == a) {
temp++;
continue;
}
if(tops[i] == a) {
cnt1++;
}
if(bottoms[i] == a) {
cnt2++;
}
}
if(cnt1 + cnt2 + temp == n - 1) {
if(tops[0] == bottoms[0]) {
return min(cnt1, cnt2);
}
return min(cnt1, cnt2 + 1);
}
return -1;
}
};