In a row of dominoes, A[i]
and B[i]
represent the top and bottom halves of the i
-th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
We may rotate the i
-th domino, so that A[i]
and B[i]
swap values.
Return the minimum number of rotations so that all the values in A
are the same, or all the values in B
are the same.
If it cannot be done, return -1
.
Example 1:
Input: A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]
Output: 2
Explanation:
The first figure represents the dominoes as given by A and B: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
Example 2:
Input: A = [3,5,1,2,3], B = [3,6,3,3,4]
Output: -1
Explanation:
In this case, it is not possible to rotate the dominoes to make one row of values equal.
Note:
1 <= A[i], B[i] <= 6
2 <= A.length == B.length <= 20000
[] []
[1] [2]
[1] [1]
[1,1,1,1] [2,2,2,2]
[4,5,5,5,4,5,5,3] [5,2,1,4,5,6,3,5]
[4,5,5,5,4,5,5,3] [5,2,1,4,5,6,3,3]
[1,2,3] [2,2,2]
[1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1] [2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2]
class Solution(object):
def minDominoRotations(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
length = len(A)
if length <= 1:
return 0
num1 = A[0]
num2 = B[0]
res = length
table1 = [0 for _ in range(length)]
table2 = [0 for _ in range(length)]
for i in range(length):
if A[i]==num1 or B[i]==num1:
table1[i] = 1
if A[i]==num2 or B[i]==num2:
table2[i] = 1
sum1 = sum(table1)
sum2 = sum(table2)
if sum1<length and sum2<length:
return -1
possibilities = []
if sum1 == length:
possibilities.append(num1)
if sum2 == length:
possibilities.append(num2)
for num in possibilities:
table3 = [0 for _ in range(length)]
table4 = [0 for _ in range(length)]
for i in range(length):
if A[i]==num:
table3[i] = 1
if B[i]==num:
table4[i] = 1
val1 = min(length-sum(table3),sum(table3))
val2 = min(length-sum(table4),sum(table4))
res = min(res,min(val1,val2))
return res
class Solution(object):
def minDominoRotations(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
length = len(A)
if length <= 1:
return 0
res = -1
possibilities = [A[0],B[0]]
for value in possibilities:
countA = 0
countB = 0
flag = True
for i in range(length):
if A[i]!=value and B[i]!=value:
flag = False
continue
if A[i]!=value:
countA += 1
elif B[i]!=value:
countB += 1
if flag:
res = min(countA,countB)
break
return res