Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

1007. Minimum Domino Rotations For Equal Row

Medium52.3% Acceptance
ArrayGreedy
Asked by:
G
Google
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)

We may rotate the ith domino, so that tops[i] and bottoms[i] swap values.

Return the minimum number of rotations so that all the values in tops are the same, or all the values in bottoms are the same.

If it cannot be done, return -1.

Example 1:

Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
Output: 2
Explanation: 
The first figure represents the dominoes as given by tops and bottoms: 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: tops = [3,5,1,2,3], bottoms = [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.

Constraints:

  • 2 <= tops.length <= 2 * 104
  • bottoms.length == tops.length
  • 1 <= tops[i], bottoms[i] <= 6
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

In #1007 Minimum Domino Rotations For Equal Row, the goal is to determine the minimum number of rotations needed so that all values in either the top row or bottom row of dominoes become the same. Each domino contains two numbers, and a rotation swaps the top and bottom values at that position.

A key observation is that the final uniform value must be one of the numbers from the first domino. Therefore, we only need to check two candidates: tops[0] and bottoms[0]. For each candidate value, iterate through the arrays and count how many rotations are required to make all values in the top row or bottom row equal to that candidate. If at any index neither side matches the candidate, that candidate is invalid.

This greedy check allows us to evaluate the feasibility and compute the minimum rotations in a single pass. The approach efficiently avoids trying all possible values and keeps the solution linear in time.

Time Complexity: O(n) for scanning the domino arrays.
Space Complexity: O(1) since only counters are used.

Complexity

ApproachTime ComplexitySpace Complexity
Greedy candidate check using first domino valuesO(n)O(1)

Video Solution Available

Kevin Naughton Jr.

View all video solutions

Solutions (12)

One-Pass Count Method

This approach is based on selecting a target number to either dominate the top or bottom row and counting the minimum rotations needed.

We first choose a target which could dominate the row: either tops[0] or bottoms[0]. Then we iterate over each domino, using an additional check to verify if that target can fully dominate a row. If we face a domino where neither side contains the target, it's not possible for that target to dominate the row.

Time Complexity: O(n), where n is the number of dominoes.
Space Complexity: O(1)

CC++JavaPythonC#JavaScript
1#include <stdbool.h>
2
3int minDominoRotations(int* tops, int topsSize, int* bottoms, int bottomsSize) {
4    int myMin(


Explanation

We try rotating to make all dominoes` number either one of the first elements from tops or bottoms. We count the rotations for both top and bottom for each target. If any domino can't be converted to the target value at any time, the approach exits early.

Max Frequency Element Method

To solve the problem using this approach, we evaluate the elements with the most appearance that could be converted to make a row uniform. This involves utilizing frequency maps to identify potential candidates quickly and checking their feasibility.

Time Complexity: O(n), where n is the number of dominoes.
Space Complexity: O(1)

CC++JavaPythonC#JavaScript
1#

Video Solutions

Watch expert explanations and walkthroughs

Minimum Domino Rotations for Equal Row

Kevin Naughton Jr.
12:3443,530 views

Asked By Companies

1 companies
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
Jump Game IIMedium
Jump GameMedium
Best Time to Buy and Sell Stock IIMedium
More similar problems

Related Topics

ArrayGreedy

Problem Stats

Acceptance Rate52.3%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why do we only check the first domino values as candidates?

If a uniform value is possible across all dominoes, that value must appear in the first domino either on the top or bottom. Otherwise, the first domino itself could never match the final value. This insight reduces the search space to just two candidates.

Is Minimum Domino Rotations For Equal Row asked in FAANG interviews?

Yes, variations of this problem have appeared in technical interviews at major tech companies. It tests greedy reasoning, observation skills, and the ability to simplify a problem by reducing candidate possibilities.

What data structure is best for Minimum Domino Rotations For Equal Row?

The problem mainly relies on arrays since the domino values are given as two arrays: tops and bottoms. No additional complex data structures are required. Simple counters and iteration are sufficient for an optimal solution.

What is the optimal approach for Minimum Domino Rotations For Equal Row?

The optimal approach uses a greedy observation that the final equal value must be either tops[0] or bottoms[0]. By checking both candidates and counting the rotations needed in a single pass, we can determine the minimum rotations efficiently. This results in an O(n) time and O(1) space solution.

int
a
,
int
b
)
{
5
return
a
<
b
?
a
:
b
;
6
}
7
8
bool
canConvert
(
int
targetValue
)
{
9
int
topRotations
=
0
,
bottomRotations
=
0
;
10
for
(
int
i
=
0
;
i
<
topsSize
;
i
++
)
{
11
if
(
tops
[
i
]
!=
targetValue
&&
bottoms
[
i
]
!=
targetValue
)
{
12
return
false
;
13
}
else
if
(
tops
[
i
]
!=
targetValue
)
{
14
topRotations
++
;
15
}
else
if
(
bottoms
[
i
]
!=
targetValue
)
{
16
bottomRotations
++
;
17
}
18
}
19
return
myMin
(
topRotations
,
bottomRotations
)
;
20
}
21
22
int
target1
=
tops
[
0
]
;
23
int
target2
=
bottoms
[
0
]
;
24
int
result1
=
canConvert
(
target1
)
?
canConvert
(
target1
)
:
-
1
;
25
int
result2
=
canConvert
(
target2
)
?
canConvert
(
target2
)
:
-
1
;
26
27
if
(
result1
==
-
1
)
return
result2
;
28
if
(
result2
==
-
1
)
return
result1
;
29
return
myMin
(
result1
,
result2
)
;
30
}
include
<limits.h>
2
3
int
minDominoRotations
(
int
*
tops
,
int
topsSize
,
int
*
bottoms
,
int
bottomsSize
)
{
4
int
myMin
(
int
a
,
int
b
)
{
5
return
a
<
b
?
a
:
b
;
6
}
7
8
int
count
[
7
]
=
{
0
}
;
9
for
(
int
i
=
0
;
i
<
topsSize
;
++
i
)
{
10
count
[
tops
[
i
]
]
++
;
11
count
[
bottoms
[
i
]
]
++
;
12
}
13
int
maxFreq
=
0
;
14
int
potential
[
]
=
{
tops
[
0
]
,
bottoms
[
0
]
}
;
15
int
total
=
0
;
16
for
(
int
i
=
0
;
i
<
2
;
++
i
)
{
17
total
=
0
;
18
for
(
int
j
=
0
;
j
<
topsSize
;
++
j
)
{
19
if
(
tops
[
j
]
!=
potential
[
i
]
&&
bottoms
[
j
]
!=
potential
[
i
]
)
{
20
total
=
-
1
;
21
break
;
22
}
23
total
+=
tops
[
j
]
!=
potential
[
i
]
;
24
}
25
if
(
total
!=
-
1
)
maxFreq
=
myMin
(
total
,
maxFreq
==
0
?
total
:
maxFreq
)
;
26
}
27
return
maxFreq
==
0
?
-
1
:
maxFreq
;
28
}

Explanation

Count the maximum appearances by any number from both top and bottom rows. Aim to make this number dominate the row by counting feasible rotations.