-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1007.cpp
More file actions
31 lines (31 loc) · 871 Bytes
/
Copy path1007.cpp
File metadata and controls
31 lines (31 loc) · 871 Bytes
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
class Solution {
public:
int minDominoRotations(vector<int> &A, vector<int> &B) {
int n = A.size();
unordered_map<int, int> count;
int max_key = 0, max_value = 0;
for (int i = 0; i < n; ++i) {
++count[A[i]];
if (B[i] != A[i])
++count[B[i]];
if (max_value < count[A[i]]) {
max_value = count[A[i]];
max_key = A[i];
}
if (max_value < count[B[i]]) {
max_value = count[B[i]];
max_key = B[i];
}
}
if (max_value < n)
return -1;
int res = 0, a = 0, b = 0;
for (int i = 0; i < n; ++i) {
if (A[i] == max_key)
++a;
if (B[i] == max_key)
++b;
}
return min(n - a, n - b);
}
};