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.
You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.
A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.
Return the length longest chain which can be formed.
You do not need to use up all the given intervals. You can select pairs in any order.
Example 1:
Input: pairs = [[1,2],[2,3],[3,4]] Output: 2 Explanation: The longest chain is [1,2] -> [3,4].
Example 2:
Input: pairs = [[1,2],[7,8],[4,5]] Output: 3 Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
Constraints:
n == pairs.length1 <= n <= 1000-1000 <= lefti < righti <= 1000Problem summary: You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti. A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion. Return the length longest chain which can be formed. You do not need to use up all the given intervals. You can select pairs in any order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Greedy
[[1,2],[2,3],[3,4]]
[[1,2],[7,8],[4,5]]
longest-increasing-subsequence)non-decreasing-subsequences)longest-non-decreasing-subarray-from-two-arrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #646: Maximum Length of Pair Chain
class Solution {
public int findLongestChain(int[][] pairs) {
Arrays.sort(pairs, (a, b) -> Integer.compare(a[1], b[1]));
int ans = 0, pre = Integer.MIN_VALUE;
for (var p : pairs) {
if (pre < p[0]) {
++ans;
pre = p[1];
}
}
return ans;
}
}
// Accepted solution for LeetCode #646: Maximum Length of Pair Chain
func findLongestChain(pairs [][]int) (ans int) {
sort.Slice(pairs, func(i, j int) bool { return pairs[i][1] < pairs[j][1] })
pre := math.MinInt
for _, p := range pairs {
if pre < p[0] {
ans++
pre = p[1]
}
}
return
}
# Accepted solution for LeetCode #646: Maximum Length of Pair Chain
class Solution:
def findLongestChain(self, pairs: List[List[int]]) -> int:
pairs.sort(key=lambda x: x[1])
ans, pre = 0, -inf
for a, b in pairs:
if pre < a:
ans += 1
pre = b
return ans
// Accepted solution for LeetCode #646: Maximum Length of Pair Chain
impl Solution {
pub fn find_longest_chain(mut pairs: Vec<Vec<i32>>) -> i32 {
pairs.sort_by_key(|pair| pair[1]);
let mut ans = 0;
let mut pre = i32::MIN;
for pair in pairs {
let (a, b) = (pair[0], pair[1]);
if pre < a {
ans += 1;
pre = b;
}
}
ans
}
}
// Accepted solution for LeetCode #646: Maximum Length of Pair Chain
function findLongestChain(pairs: number[][]): number {
pairs.sort((a, b) => a[1] - b[1]);
let [ans, pre] = [0, -Infinity];
for (const [a, b] of pairs) {
if (pre < a) {
++ans;
pre = b;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
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.