-
Notifications
You must be signed in to change notification settings - Fork 0
/
1007.minDomino.cpp
43 lines (43 loc) · 1.04 KB
/
1007.minDomino.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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
// class Solution {
// public:
int minDominoRotations(vector<int>& A, vector<int>& B) {
map<int,int> m;
for(int num : A){
++m[num];
}
for(int num : B){
++m[num];
}
int max_count = 0;
int target;
for(auto it = m.begin(); it != m.end(); it++){
if(it->second >= max_count){
max_count = it->second;
target = it->first;
}
}
int a=0;
int b=0;
for(int i = 0; i < A.size(); i++){
if(A[i]!=target && B[i]!=target){
return -1;
}
a=(A[i]==target)?++a:a;
b=(B[i]==target)?++b:b;
}
return min(A.size()-a,B.size()-b);
}
// };
int main(){
int n[] = {1, 2, 1, 1, 1,2,2,2};
int m[] = {2, 1, 2, 2, 2,2,2,2};
// vector初始化
vector<int> a(n, n+8);
vector<int> b(m, m+8);
cout<<minDominoRotations(a,b)<<endl;
return 0;
}