Longest Increasing Path in a Matrix
Problem Statement
You are given an m × n grid of integers. From a cell you may step up, down, left or right to a neighbouring cell. Diagonal steps are not allowed, and you cannot leave the grid.
A path is increasing if every step goes to a cell with a strictly bigger value. Find the longest increasing path that starts from any cell, and return its length, counted in cells.
matrix, an m × n grid of integers.
The number of cells in the longest strictly increasing path.
The longest path is 1 → 2 → 6 → 9. Each step moves to a neighbouring cell with a bigger value.
The path 3 → 4 → 5 → 6 runs along the top row and then down the right side. Diagonal steps are not allowed.
A single cell is a path of length 1.
- • m == matrix.length
- • n == matrix[i].length
- • 1 ≤ m, n ≤ 200
- • 0 ≤ matrix[i][j] ≤ 2^31 − 1
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
From each cell, try every bigger neighbour again and again; nothing is saved between searches
Save each cell's longest path; any later search that reaches the cell reuses the saved value
Start a DFS at every cell. From the current cell, move to each neighbour with a bigger value and continue from there. Keep the longest path found. Nothing is remembered, so the same cells are explored again from every path that reaches them.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Topological Sort translate into production architectures and low-level memory mechanics.