Skip to main content

Longest Increasing Path in a Matrix


Longest Increasing Path in a Matrix: 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:

image

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:

image

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.length
  • n == matrix[i].length
  • 1 <= m, n <= 200
  • 0 <= matrix[i][j] <= 2^31 - 1

Try this Problem on your own or check similar problems:

  1. Number of Increasing Paths in a Grid
Solution:
class Solution {
int[][] directions = new int[][] { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }};
public int longestIncreasingPath(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[][] dp = new int[m][n];
int maxLength = 1;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
maxLength = Math.max(maxLength, dfs(matrix, i, j, dp));
}
}
return maxLength;
}

public int dfs(int[][] matrix, int row, int col, int[][] dp){
if(dp[row][col] != 0) return dp[row][col];

int maxLength = 1;

for(int[] d: directions){
int newRow = row + d[0];
int newCol = col + d[1];
if(newRow < 0 || newRow >= matrix.length || newCol < 0 || newCol >= matrix[0].length || matrix[newRow][newCol] <= matrix[row][col]) continue;
maxLength = Math.max(maxLength, 1 + dfs(matrix, newRow, newCol, dp));
}

dp[row][col] = maxLength;
return maxLength;
}
}

Time/Space Complexity:
  • Time Complexity: O(mn)
  • Space Complexity: O(mn)

Explanation:

We have a classic depth first search, so our time complexity is O(mn), we also store the max length for each of the cell leading to the same space complexity. From each cell we run a DFS search and check if we can increase the maximum length. We use directions array to search for neighboring matrix cells and only continue our search from the cells leading to increasing sequence. We also store the max length found for the cell in dp since we could revisit the cell in searches from other cells, so we're preventing an infinitive loop by returning early if(dp[row][col] != 0) return dp[row][col];. Why don't we have a visited array to mark the cells visited so we don't end up with a cycle coming back to the initial cell we began our search from? Note that we're only continuing our search if the next cell is larger than the previous, so we can't go back to the initial cell, since the path is strictly increasing (for example we can't have 1->2->1 since we will never go to 1 from 2 because 1 < 2). Time and space complexity are O(mn) since we visit all elements in the matrix, while for space we have additional data structure holding maximum lengh for each element in the matrix.