Skip to content

Longest Increasing Path in a Matrix

HardDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

Given an m × n integer matrix, return the length of the longest strictly increasing path. From a cell you may move up, down, left or right (no diagonals, and no wrapping around the edges). Each step must go to a strictly larger value.

matrix = [[9, 9, 4],
          [6, 6, 8],
          [2, 1, 1]]   →  4   (1 → 2 → 6 → 9)
 
matrix = [[3, 4, 5],
          [3, 2, 6],
          [2, 2, 1]]   →  4   (3 → 4 → 5 → 6)

Constraints

  • 1 <= m, n <= 200 where the matrix is m × n
  • 0 <= matrix[r][c] <= 2^31 - 1