Greedy Algorithms - Minimum Domino Rotations
Given the following code and input arrays, what is the final output printed?
def minDominoRotations(A, B):
def check(x):
rotations_a = rotations_b = 0
for i in range(len(A)):
if A[i] != x and B[i] != x:
return -1
elif A[i] != x:
rotations_a += 1
elif B[i] != x:
rotations_b += 1
return min(rotations_a, rotations_b)
rotations = check(A[0])
if rotations != -1:
return rotations
else:
return check(B[0])
A = [3,5,1,2]
B = [3,6,3,3]
print(minDominoRotations(A, B))