Length of the Longest Increasing Path - Problem

You're navigating through a 2D coordinate system where you need to find the longest possible increasing path that passes through a specific point!

Given an array of coordinates representing points in a 2D plane and an index k, your mission is to find the maximum length of an increasing path that contains the point at coordinates[k].

An increasing path is a sequence of points (x₁, y₁) → (x₂, y₂) → ... → (xₘ, yₘ) where:

  • Both x and y coordinates strictly increase: xᵢ < xᵢ₊₁ and yᵢ < yᵢ₊₁
  • All points must exist in the given coordinates array
  • The path must include coordinates[k]

Example: If coordinates = [[1,2], [2,3], [3,1], [0,1]] and k = 1, the point (2,3) must be included in your longest increasing path.

Input & Output

example_1.py — Basic Path
$ Input: coordinates = [[1,2], [2,3], [3,1], [0,1]], k = 1
Output: 2
💡 Note: Point k is (2,3). The longest increasing path containing (2,3) has length 2: either [(1,2), (2,3)] or [(2,3), (any point with x>2 and y>3 if exists)]. Since no point satisfies x>2 and y>3, the longest path is [(1,2), (2,3)].
example_2.py — Longer Path
$ Input: coordinates = [[1,1], [2,2], [3,3], [4,4]], k = 1
Output: 4
💡 Note: Point k is (2,2). The longest increasing path is [(1,1), (2,2), (3,3), (4,4)] which contains (2,2) and has length 4.
example_3.py — Single Point
$ Input: coordinates = [[5,10]], k = 0
Output: 1
💡 Note: Only one point exists, so the longest path containing coordinates[0] = (5,10) has length 1.

Constraints

  • 1 ≤ n ≤ 1000
  • 0 ≤ k < n
  • coordinates[i].length == 2
  • -105 ≤ coordinates[i][0], coordinates[i][1] ≤ 105
  • All coordinates are distinct

Visualization

Tap to expand
🏔️ Mountain Path Through Checkpoint(1,5)Checkpoint K(5,8)(7,9)(9,10)🎯 Longest path through checkpoint: 2 (to checkpoint) + 4 (from checkpoint) - 1 = 5
Understanding the Visualization
1
Organize the Peaks
Sort all mountain peaks by their coordinates to process them systematically
2
Calculate Ascent Routes
For each peak, find the longest climbing route that ends at that peak
3
Calculate Descent Routes
For each peak, find the longest climbing route that starts from that peak
4
Find Optimal Path Through Checkpoint
Combine the best ascent to checkpoint k with the best route from checkpoint k
Key Takeaway
🎯 Key Insight: The longest path through any point equals the longest path ending at that point plus the longest path starting from that point, minus 1 (to avoid double-counting the point itself).
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 25
28.4K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen