Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 * 104bottoms.length == tops.length1 <= tops[i], bottoms[i] <= 6Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[2,1,2,4,2,2] [5,2,6,2,3,2]
[3,5,1,2,3] [3,6,3,3,4]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1007: Minimum Domino Rotations For Equal Row
class Solution {
private int n;
private int[] tops;
private int[] bottoms;
public int minDominoRotations(int[] tops, int[] bottoms) {
n = tops.length;
this.tops = tops;
this.bottoms = bottoms;
int ans = Math.min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}
private int f(int x) {
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (tops[i] != x && bottoms[i] != x) {
return n + 1;
}
cnt1 += tops[i] == x ? 1 : 0;
cnt2 += bottoms[i] == x ? 1 : 0;
}
return n - Math.max(cnt1, cnt2);
}
}
// Accepted solution for LeetCode #1007: Minimum Domino Rotations For Equal Row
func minDominoRotations(tops []int, bottoms []int) int {
n := len(tops)
f := func(x int) int {
cnt1, cnt2 := 0, 0
for i, a := range tops {
b := bottoms[i]
if a != x && b != x {
return n + 1
}
if a == x {
cnt1++
}
if b == x {
cnt2++
}
}
return n - max(cnt1, cnt2)
}
ans := min(f(tops[0]), f(bottoms[0]))
if ans > n {
return -1
}
return ans
}
# Accepted solution for LeetCode #1007: Minimum Domino Rotations For Equal Row
class Solution:
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
def f(x: int) -> int:
cnt1 = cnt2 = 0
for a, b in zip(tops, bottoms):
if x not in (a, b):
return inf
cnt1 += a == x
cnt2 += b == x
return len(tops) - max(cnt1, cnt2)
ans = min(f(tops[0]), f(bottoms[0]))
return -1 if ans == inf else ans
// Accepted solution for LeetCode #1007: Minimum Domino Rotations For Equal Row
impl Solution {
pub fn min_domino_rotations(tops: Vec<i32>, bottoms: Vec<i32>) -> i32 {
let n = tops.len() as i32;
let f = |x: i32| -> i32 {
let mut cnt1 = 0;
let mut cnt2 = 0;
for i in 0..n as usize {
if tops[i] != x && bottoms[i] != x {
return n + 1;
}
if tops[i] == x {
cnt1 += 1;
}
if bottoms[i] == x {
cnt2 += 1;
}
}
n - cnt1.max(cnt2)
};
let ans = f(tops[0]).min(f(bottoms[0]));
if ans > n {
-1
} else {
ans
}
}
}
// Accepted solution for LeetCode #1007: Minimum Domino Rotations For Equal Row
function minDominoRotations(tops: number[], bottoms: number[]): number {
const n = tops.length;
const f = (x: number): number => {
let [cnt1, cnt2] = [0, 0];
for (let i = 0; i < n; ++i) {
if (tops[i] !== x && bottoms[i] !== x) {
return n + 1;
}
cnt1 += tops[i] === x ? 1 : 0;
cnt2 += bottoms[i] === x ? 1 : 0;
}
return n - Math.max(cnt1, cnt2);
};
const ans = Math.min(f(tops[0]), f(bottoms[0]));
return ans > n ? -1 : ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.