-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem-1007.cpp
40 lines (37 loc) · 1004 Bytes
/
Problem-1007.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
// Problem - 1007
// https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/
// O(n) time complexity and O(1) space complexity solution
class Solution {
public:
int minDominoRotations(vector<int>& A, vector<int>& B) {
int n = A.size();
int ans = n+1;
int a = 0, b = 0;
for(int i = 0; i < n; i++) {
if(A[0] != A[i] && A[0] != B[i])
break;
if(A[i] != A[0]) {
a++;
}
if(B[i] != A[0]) {
b++;
}
if(i == n-1)
ans = min({ans, a, b});
}
a = b = 0;
for(int i = 0; i < n; i++) {
if(B[0] != A[i] && B[0] != B[i])
break;
if(A[i] != B[0]) {
a++;
}
if(B[i] != B[0]) {
b++;
}
if(i == n-1)
ans = min({ans, a, b});
}
return ans == n+1 ? -1 : ans;
}
};