Length of the Longest Increasing Path - Problem

You are given a 2D array of integers coordinates of length n and an integer k, where 0 <= k < n. coordinates[i] = [x_i, y_i] indicates the point (x_i, y_i) in a 2D plane.

An increasing path of length m is defined as a list of points (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_m, y_m) such that:

  • x_i < x_{i+1} and y_i < y_{i+1} for all i where 1 <= i < m
  • (x_i, y_i) is in the given coordinates for all i where 1 <= i <= m

Return the maximum length of an increasing path that contains coordinates[k].

Input & Output

Example 1 — Basic Increasing Path
$ Input: coordinates = [[1,1],[2,3],[4,2],[5,4]], k = 0
Output: 3
💡 Note: Starting from coordinates[0] = (1,1), we can form path (1,1) → (2,3) → (5,4) with length 3
Example 2 — Middle Point
$ Input: coordinates = [[1,1],[2,3],[4,2],[5,4]], k = 1
Output: 2
💡 Note: Starting from coordinates[1] = (2,3), we can form path (2,3) → (5,4) with length 2
Example 3 — Single Point
$ Input: coordinates = [[3,4]], k = 0
Output: 1
💡 Note: Only one point exists, so maximum path length is 1

Constraints

  • 1 ≤ coordinates.length ≤ 103
  • 0 ≤ k < coordinates.length
  • coordinates[i].length == 2
  • -104 ≤ coordinates[i][0], coordinates[i][1] ≤ 104

Visualization

Tap to expand
Longest Increasing Path in 2D CoordinatesInput: coordinates = [[1,1],[2,3],[4,2],[5,4]], k = 0(1,1)k=0(2,3)(4,2)(5,4)Path: (1,1) → (2,3) → (5,4)Each step: x₍ᵢ₊₁₎ > xᵢ and y₍ᵢ₊₁₎ > yᵢMaximum Length: 3
Understanding the Visualization
1
Input Points
Given coordinates and target index k
2
Find Paths
Explore all increasing paths from coordinates[k]
3
Output
Return maximum path length found
Key Takeaway
🎯 Key Insight: Use dynamic programming to efficiently calculate the longest increasing path from each point, avoiding redundant calculations through memoization
Asked in
Google 25 Microsoft 18 Amazon 15
26.3K Views
Medium Frequency
~25 min Avg. Time
847 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