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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given a 2D array of integers coordinates of length n and an integer k, where 0 <= k < n.
coordinates[i] = [xi, yi] indicates the point (xi, yi) in a 2D plane.
An increasing path of length m is defined as a list of points (x1, y1), (x2, y2), (x3, y3), ..., (xm, ym) such that:
xi < xi + 1 and yi < yi + 1 for all i where 1 <= i < m.(xi, yi) is in the given coordinates for all i where 1 <= i <= m.Return the maximum length of an increasing path that contains coordinates[k].
Example 1:
Input: coordinates = [[3,1],[2,2],[4,1],[0,0],[5,3]], k = 1
Output: 3
Explanation:
(0, 0), (2, 2), (5, 3) is the longest increasing path that contains (2, 2).
Example 2:
Input: coordinates = [[2,1],[7,0],[5,6]], k = 2
Output: 2
Explanation:
(2, 1), (5, 6) is the longest increasing path that contains (5, 6).
Constraints:
1 <= n == coordinates.length <= 105coordinates[i].length == 20 <= coordinates[i][0], coordinates[i][1] <= 109coordinates are distinct.0 <= k <= n - 1Problem summary: You are given a 2D array of integers coordinates of length n and an integer k, where 0 <= k < n. coordinates[i] = [xi, yi] indicates the point (xi, yi) in a 2D plane. An increasing path of length m is defined as a list of points (x1, y1), (x2, y2), (x3, y3), ..., (xm, ym) such that: xi < xi + 1 and yi < yi + 1 for all i where 1 <= i < m. (xi, yi) is in the given coordinates for all i where 1 <= i <= m. Return the maximum length of an increasing path that contains coordinates[k].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[[3,1],[2,2],[4,1],[0,0],[5,3]] 1
[[2,1],[7,0],[5,6]] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3288: Length of the Longest Increasing Path
class Solution {
public int maxPathLength(int[][] coordinates, int k) {
final int xk = coordinates[k][0];
final int yk = coordinates[k][1];
List<int[]> leftCoordinates = new ArrayList<>();
List<int[]> rightCoordinates = new ArrayList<>();
for (int[] coordinate : coordinates) {
final int x = coordinate[0];
final int y = coordinate[1];
if (x < xk && y < yk)
leftCoordinates.add(new int[] {x, y});
else if (x > xk && y > yk)
rightCoordinates.add(new int[] {x, y});
}
return 1 + lengthOfLIS(leftCoordinates) + lengthOfLIS(rightCoordinates);
}
// Similar to 300. Longest Increasing Subsequence
private int lengthOfLIS(List<int[]> coordinates) {
coordinates.sort(Comparator.comparingInt((int[] coordinate) -> coordinate[0])
.thenComparingInt((int[] coordinate) -> - coordinate[1]));
// tails[i] := the minimum tail of all the increasing subsequences having
// length i + 1
List<Integer> tails = new ArrayList<>();
for (int[] coordinate : coordinates) {
final int y = coordinate[1];
if (tails.isEmpty() || y > tails.get(tails.size() - 1))
tails.add(y);
else
tails.set(firstGreaterEqual(tails, y), y);
}
return tails.size();
}
private int firstGreaterEqual(List<Integer> arr, int target) {
final int i = Collections.binarySearch(arr, target);
return i < 0 ? -i - 1 : i;
}
}
// Accepted solution for LeetCode #3288: Length of the Longest Increasing Path
package main
import (
"cmp"
"slices"
"sort"
)
// https://space.bilibili.com/206214
func maxPathLength(coordinates [][]int, k int) int {
kx, ky := coordinates[k][0], coordinates[k][1]
slices.SortFunc(coordinates, func(a, b []int) int { return cmp.Or(a[0]-b[0], b[1]-a[1]) })
g := []int{}
for _, p := range coordinates {
x, y := p[0], p[1]
if x < kx && y < ky || x > kx && y > ky {
j := sort.SearchInts(g, y)
if j < len(g) {
g[j] = y
} else {
g = append(g, y)
}
}
}
return len(g) + 1 // 算上 coordinates[k]
}
# Accepted solution for LeetCode #3288: Length of the Longest Increasing Path
class Solution:
def maxPathLength(self, coordinates: list[list[int]], k: int) -> int:
xk, yk = coordinates[k]
leftCoordinates = [(x, y) for x, y in coordinates if x < xk and y < yk]
rightCoordinates = [(x, y) for x, y in coordinates if x > xk and y > yk]
return (1 +
self._lengthOfLIS(leftCoordinates) +
self._lengthOfLIS(rightCoordinates))
# Similar to 300. Longest Increasing Subsequence
def _lengthOfLIS(self, coordinates: list[tuple[int, int]]) -> int:
coordinates.sort(key=lambda x: (x[0], -x[1]))
# tail[i] := the minimum tail of all the increasing subsequences having
# length i + 1
tail = []
for _, y in coordinates:
if not tail or y > tail[-1]:
tail.append(y)
else:
tail[bisect.bisect_left(tail, y)] = y
return len(tail)
// Accepted solution for LeetCode #3288: Length of the Longest Increasing Path
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3288: Length of the Longest Increasing Path
// class Solution {
// public int maxPathLength(int[][] coordinates, int k) {
// final int xk = coordinates[k][0];
// final int yk = coordinates[k][1];
// List<int[]> leftCoordinates = new ArrayList<>();
// List<int[]> rightCoordinates = new ArrayList<>();
//
// for (int[] coordinate : coordinates) {
// final int x = coordinate[0];
// final int y = coordinate[1];
// if (x < xk && y < yk)
// leftCoordinates.add(new int[] {x, y});
// else if (x > xk && y > yk)
// rightCoordinates.add(new int[] {x, y});
// }
//
// return 1 + lengthOfLIS(leftCoordinates) + lengthOfLIS(rightCoordinates);
// }
//
// // Similar to 300. Longest Increasing Subsequence
// private int lengthOfLIS(List<int[]> coordinates) {
// coordinates.sort(Comparator.comparingInt((int[] coordinate) -> coordinate[0])
// .thenComparingInt((int[] coordinate) -> - coordinate[1]));
// // tails[i] := the minimum tail of all the increasing subsequences having
// // length i + 1
// List<Integer> tails = new ArrayList<>();
// for (int[] coordinate : coordinates) {
// final int y = coordinate[1];
// if (tails.isEmpty() || y > tails.get(tails.size() - 1))
// tails.add(y);
// else
// tails.set(firstGreaterEqual(tails, y), y);
// }
// return tails.size();
// }
//
// private int firstGreaterEqual(List<Integer> arr, int target) {
// final int i = Collections.binarySearch(arr, target);
// return i < 0 ? -i - 1 : i;
// }
// }
// Accepted solution for LeetCode #3288: Length of the Longest Increasing Path
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3288: Length of the Longest Increasing Path
// class Solution {
// public int maxPathLength(int[][] coordinates, int k) {
// final int xk = coordinates[k][0];
// final int yk = coordinates[k][1];
// List<int[]> leftCoordinates = new ArrayList<>();
// List<int[]> rightCoordinates = new ArrayList<>();
//
// for (int[] coordinate : coordinates) {
// final int x = coordinate[0];
// final int y = coordinate[1];
// if (x < xk && y < yk)
// leftCoordinates.add(new int[] {x, y});
// else if (x > xk && y > yk)
// rightCoordinates.add(new int[] {x, y});
// }
//
// return 1 + lengthOfLIS(leftCoordinates) + lengthOfLIS(rightCoordinates);
// }
//
// // Similar to 300. Longest Increasing Subsequence
// private int lengthOfLIS(List<int[]> coordinates) {
// coordinates.sort(Comparator.comparingInt((int[] coordinate) -> coordinate[0])
// .thenComparingInt((int[] coordinate) -> - coordinate[1]));
// // tails[i] := the minimum tail of all the increasing subsequences having
// // length i + 1
// List<Integer> tails = new ArrayList<>();
// for (int[] coordinate : coordinates) {
// final int y = coordinate[1];
// if (tails.isEmpty() || y > tails.get(tails.size() - 1))
// tails.add(y);
// else
// tails.set(firstGreaterEqual(tails, y), y);
// }
// return tails.size();
// }
//
// private int firstGreaterEqual(List<Integer> arr, int target) {
// final int i = Collections.binarySearch(arr, target);
// return i < 0 ? -i - 1 : i;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.