-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq1007.hpp
More file actions
89 lines (83 loc) · 2.44 KB
/
q1007.hpp
File metadata and controls
89 lines (83 loc) · 2.44 KB
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
//
// q1007.hpp
// LeetCode
//
// Created by 李凌 on 2020/10/19.
// Copyright © 2020 DoubleL. All rights reserved.
//
#ifndef q1007_hpp
#define q1007_hpp
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
void question1007(){
class Solution {
public:
int minDominoRotations(vector<int>& A, vector<int>& B) {
unordered_map<int, int> map;
int count1 = 0;
int count2 = 0;
int common1 = 0;
int common2 = 0;
map[A[0]] = 0;
map[B[0]] = 0;
for (int i = 0; i < A.size(); i++) {
int val1 = A[i];
int val2 = B[i];
if (map.find(val1) == map.end() && map.find(val2) == map.end()) {
return -1;
}
if (map.find(val1) != map.end()) {
map[val1]++;
}
if (map.find(val2) != map.end()) {
map[val2]++;
}
if (val1 == val2) {
map[val1]--;
if (val1 == A[0]) {
common1++;
}else{
common2++;
}
}
if (map[A[0]] <= i && map[B[0]] <= i) {
return -1;
}
if (val1 == A[0]) {
count1++;
}
if (val2 == B[0]) {
count2++;
}
}
if (map.begin()->second == A.size()) {
if (map.begin()->first == A[0]) {
return min(count1 - common1, (int)A.size() - count1);
}else{
return min(count2 - common2, (int)B.size() - count2);
}
}else{
if (map.begin()->first == A[0]) {
return min(count2 - common2, (int)B.size() - count2);
}else{
return min(count1 - common1, (int)A.size() - count1);
}
}
return -1;
}
};
vector<int> A = {2,1,1,3,2,1,2,2,1};
vector<int> B = {3,2,3,1,3,2,3,3,2};
int result = Solution().minDominoRotations(A, B);
printf("result %d\n", result);
}
#endif /* q1007_hpp */