Using greedy without proof
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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
There are n couples sitting in 2n seats arranged in a row and want to hold hands.
The people and seats are represented by an integer array row where row[i] is the ID of the person sitting in the ith seat. The couples are numbered in order, the first couple being (0, 1), the second couple being (2, 3), and so on with the last couple being (2n - 2, 2n - 1).
Return the minimum number of swaps so that every couple is sitting side by side. A swap consists of choosing any two people, then they stand up and switch seats.
Example 1:
Input: row = [0,2,1,3] Output: 1 Explanation: We only need to swap the second (row[1]) and third (row[2]) person.
Example 2:
Input: row = [3,2,0,1] Output: 0 Explanation: All couples are already seated side by side.
Constraints:
2n == row.length2 <= n <= 300 <= row[i] < 2nrow are unique.Problem summary: There are n couples sitting in 2n seats arranged in a row and want to hold hands. The people and seats are represented by an integer array row where row[i] is the ID of the person sitting in the ith seat. The couples are numbered in order, the first couple being (0, 1), the second couple being (2, 3), and so on with the last couple being (2n - 2, 2n - 1). Return the minimum number of swaps so that every couple is sitting side by side. A swap consists of choosing any two people, then they stand up and switch seats.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy · Union-Find
[0,2,1,3]
[3,2,0,1]
first-missing-positive)missing-number)k-similar-strings)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #765: Couples Holding Hands
class Solution {
private int[] p;
public int minSwapsCouples(int[] row) {
int n = row.length >> 1;
p = new int[n];
for (int i = 0; i < n; ++i) {
p[i] = i;
}
for (int i = 0; i < n << 1; i += 2) {
int a = row[i] >> 1, b = row[i + 1] >> 1;
p[find(a)] = find(b);
}
int ans = n;
for (int i = 0; i < n; ++i) {
if (i == find(i)) {
--ans;
}
}
return ans;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
// Accepted solution for LeetCode #765: Couples Holding Hands
func minSwapsCouples(row []int) int {
n := len(row) >> 1
p := make([]int, n)
for i := range p {
p[i] = i
}
var find func(int) int
find = func(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}
for i := 0; i < n<<1; i += 2 {
a, b := row[i]>>1, row[i+1]>>1
p[find(a)] = find(b)
}
ans := n
for i := range p {
if find(i) == i {
ans--
}
}
return ans
}
# Accepted solution for LeetCode #765: Couples Holding Hands
class Solution:
def minSwapsCouples(self, row: List[int]) -> int:
def find(x: int) -> int:
if p[x] != x:
p[x] = find(p[x])
return p[x]
n = len(row) >> 1
p = list(range(n))
for i in range(0, len(row), 2):
a, b = row[i] >> 1, row[i + 1] >> 1
p[find(a)] = find(b)
return n - sum(i == find(i) for i in range(n))
// Accepted solution for LeetCode #765: Couples Holding Hands
struct Solution;
impl Solution {
fn min_swaps_couples(mut row: Vec<i32>) -> i32 {
let n = row.len();
let mut res = 0;
for i in 0..n {
if i % 2 == 1 {
continue;
}
if row[i] == row[i + 1] ^ 1 {
continue;
}
res += 1;
for j in i + 2..n {
if row[i] == row[j] ^ 1 {
row.swap(i + 1, j);
break;
}
}
}
res
}
}
#[test]
fn test() {
let row = vec![0, 2, 1, 3];
let res = 1;
assert_eq!(Solution::min_swaps_couples(row), res);
let row = vec![3, 2, 0, 1];
let res = 0;
assert_eq!(Solution::min_swaps_couples(row), res);
}
// Accepted solution for LeetCode #765: Couples Holding Hands
function minSwapsCouples(row: number[]): number {
const n = row.length >> 1;
const p: number[] = Array(n)
.fill(0)
.map((_, i) => i);
const find = (x: number): number => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};
for (let i = 0; i < n << 1; i += 2) {
const a = row[i] >> 1;
const b = row[i + 1] >> 1;
p[find(a)] = find(b);
}
let ans = n;
for (let i = 0; i < n; ++i) {
if (i === find(i)) {
--ans;
}
}
return 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: 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.