Repository navigation
Expand file tree
/
Copy path1007.cpp
More file actions
70 lines (66 loc) · 1.38 KB
/
Copy path1007.cpp
File metadata and controls
70 lines (66 loc) · 1.38 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
#include <vector>
using namespace std;
class Solution
{
public:
int minDominoRotations(vector<int> &tops, vector<int> &bottoms)
{
int n = tops.size();
int a = topToBot(tops, bottoms, n);
if (a == -1)
{
if (tops[0] == bottoms[0])
{
return -1;
}
else
{
return topToBot(bottoms, tops, n);
}
}
return a;
}
private:
int topToBot(vector<int> &tops, vector<int> &bottoms, int n)
{
int top = tops[0];
int bot = bottoms[0];
int a = 0;
int b = 0;
if (top != bot)
{
a = 1;
}
for (int i = 1; i < n; i++)
{
if (tops[i] == top)
{
if (bottoms[i] != top)
{
a++;
}
}
else
{
if (bottoms[i] == top)
{
b++;
}
else
{
return -1;
}
}
}
return a < b ? a : b;
}
};
vector<int> tops = {2, 1, 2, 4, 2, 2}; // 2
vector<int> bottoms = {5, 2, 6, 2, 3, 2};
int main()
{
Solution sol;
int ans = sol.minDominoRotations(tops, bottoms);
printf("ans = %d\n", ans);
return 0;
}