Minimum Domino Rotations For Equal Row

A row of dominoes is described by `tops[i]` and `bottoms[i]`, the top and bottom halves of the `i`th domino (each a number from 1 to 6). One rotation swaps the two halves of a single domino. Return the minimum number of rotations that makes every value in `tops` equal, or every value in `bottoms` equal. If neither is possible, return `-1`.

30:00

Minimum Domino Rotations For Equal Row
medium
Free written solution
Topics
Companies

A row of dominoes is described by tops[i] and bottoms[i], the top and bottom halves of the ith domino (each a number from 1 to 6). One rotation swaps the two halves of a single domino. Return the minimum number of rotations that makes every value in tops equal, or every value in bottoms equal. If neither is possible, return -1.

Example 1:
Input: {"tops":[2,1,2,4,2,2],"bottoms":[5,2,6,2,3,2]}
Output: 2
Constraints:
  • 2≤tops.length≤2×1042 \leq \text{tops.length} \leq 2 \times 10^4

  • bottoms.length=tops.length\text{bottoms.length} = \text{tops.length}

  • 1≤tops[i],bottoms[i]≤61 \leq \text{tops}[i], \text{bottoms}[i] \leq 6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"tops":[2,1,2,4,2,2],"bottoms":[5,2,6,2,3,2]}
target x = ?

0

1

2

3

4

5

top row all x

0 rotations

↻ above a domino

bottom row all x

0 rotations

↻ below a domino

6 dominoes. A rotation only swaps the two halves of one domino, so a value can fill a row only if every domino already carries it.

Variables
VariableValue
rot_top0
rot_bottom0
DepthFunction Call
Stack empty
0/8