Given an m x n integers matrix, return the length of the longest increasing path in matrix.
From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).
Example 1:
Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].
Example 2:
Input: matrix = [[3,4,5],[3,2,6],[2,2,1]]
Output: 4
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
Example 3:
Input: matrix = [[1]] Output: 1
Constraints:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 2000 <= matrix[i][j] <= 231 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are looking at a grid of numbers and need to find the longest path you can take by moving from number to larger number. The brute force strategy involves testing every single possible path starting from every single spot on the board to see how far you can go.
Here's how the algorithm would work step-by-step:
def longestIncreasingPath(matrix):
if not matrix or not matrix[0]:
return 0
row_count = len(matrix)
col_count = len(matrix[0])
def explore_paths(current_row, current_col):
max_path_from_here = 1
row_offsets = [-1, 1, 0, 0]
col_offsets = [0, 0, -1, 1]
# Check all four adjacent directions to find valid strictly increasing neighbors
for direction_index in range(4):
next_row = current_row + row_offsets[direction_index]
next_col = current_col + col_offsets[direction_index]
if 0 <= next_row < row_count and 0 <= next_col < col_count:
if matrix[next_row][next_col] > matrix[current_row][current_col]:
candidate_path = 1 + explore_paths(next_row, next_col)
if candidate_path > max_path_from_here:
max_path_from_here = candidate_path
return max_path_from_here
overall_max_length = 0
# Evaluate every single cell in the grid as a starting point for potential paths
for row_index in range(row_count):
for col_index in range(col_count):
path_length = explore_paths(row_index, col_index)
if path_length > overall_max_length:
overall_max_length = path_length
return overall_max_lengthThe optimal strategy involves exploring every possible path of increasing numbers starting from every single spot in the grid. To avoid doing the same exhausting work over and over, we remember the longest path found from any spot we have already visited so we can instantly reuse that information later.
Here's how the algorithm would work step-by-step:
| Case | How to Handle |
|---|---|
| Empty or null matrix input | Return zero immediately as no path can exist in an empty grid. |
| Matrix with dimensions 1x1 | Return one since a single cell constitutes a valid increasing path of length one. |
| Matrix with all identical values | Return one because strictly increasing conditions prevent moving to adjacent cells with equal values. |
| Matrix with negative numbers and mixed signs | Standard strict inequality checks work naturally with negative numbers without requiring coordinate shifts. |
| Graph cycles in the search space | Memoization combined with strict increasing constraints naturally prevents cycles since you can never revisit a smaller or equal value. |
| Maximum sized constraints causing deep recursion | DFS with memoization prevents exponential explosion by computing each cell at most once, running well within stack limits. |
| Strictly decreasing matrix or V-shaped valleys | The algorithm explores all four directions from every starting point and caches results to find the globally optimal path. |
| Extreme integer values at the boundaries of 32-bit limits | Value comparisons use standard arithmetic which safely handles any valid integer range without overflow risks. |