Skip to content
Decoder

Longest Increasing Path in a Matrix

Hard

Problem

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.

Example 1

matrix = [[9, 9, 4],

Example 2

          [6, 6, 8],

Example 3

Input
[2, 1, 1]]
Output
4
Why
1 → 2 → 6 → 9

Example 4

matrix = [[3, 4, 5],

Example 5

          [3, 2, 6],

Example 6

Input
[2, 2, 1]]
Output
4
Why
3 → 4 → 5 → 6

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.