Minimum Domino Rotations For Equal Row

MediumGreedygreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A row of n dominoes is described by two arrays: tops[i] and bottoms[i] are the numbers (1 to 6) on the top and bottom halves of domino i. A rotation of domino i swaps tops[i] and bottoms[i]. Return the minimum number of rotations needed so that every value in tops is the same, or every value in bottoms is the same. If neither is achievable, return -1.

Examples

Input: tops = [3,1,3,3,4,3], bottoms = [2,3,6,3,3,5]
Output: 2
Every domino shows a 3 somewhere. Rotating dominoes 1 and 4 makes the top row all 3s.
Input: tops = [1,2,1], bottoms = [2,3,3]
Output: -1
Domino 1 has no 1, and domino 2 has no 2, so no value appears on every domino.

Constraints

  • 1 <= n <= 2 * 10^4
  • `tops.length == bottoms.length == n`
  • 1 <= tops[i], bottoms[i] <= 6

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems