Last Updated: 24 Dec, 2020

Longest Increasing Path In A 2D Matrix

Hard
Asked in companies
PhonePeSamsungFacebook

Problem statement

You have been given a MATRIX of non-negative integers of size N x M where 'N' and 'M' denote the number of rows and columns, respectively.

Your task is to find the length of the longest increasing path when you can move to either four directions: left, right, up or down from each cell. Moving diagonally or outside the boundary is not allowed.

Note: A sequence of integers is said to form an increasing path in the matrix if the integers when traversed along the allowed directions can be arranged in strictly increasing order. The length of an increasing path is the number of integers in that path.

For example :

3 2 2
5 6 6
9 5 11 

In the given matrix, 3 →  5 →  6 and 3 →  5 →  9 form increasing paths of length 3 each.
Input Format :
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.

The first line of each test case contains two space-separated integers 'N', 'M' where 'N' and 'M' denote the number of rows and columns of the matrix, respectively.

From the second line of each test case, the next 'N' lines represent the rows of the MATRIX. Every row contains 'M' single space-separated integers.
Output Format :
For each test case, print the length of the longest increasing path.

Print the output of each test case in a separate line.

Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 100
1 <= N <= 50
1 <= M <= 50
0 <= MATRIX[i] <= 10^5

Time Limit: 1sec

Approaches

01 Approach

The idea is to use DFS to find the length of the increasing path for every element. 

 

We traverse the matrix and find the length of the longest increasing path starting from each cell. 

 

  1. For each element, check if it is valid to move in the four directions one by one and skip cells that are out of boundary or for which mat[x][y] > mat[dx][dy].
    1. If valid, we perform DFS for the new cell. The longest length of the path is updated during search and the final answer is found when the search is finished.
    2. Else, we return 1 as answer.
  2. We do this for all cells and keep track of maximum length found so far. Finally, we return it as our answer.

02 Approach

In the previous approach, the longest path starting from each cell is calculated again every time that cell is revisited. So, the idea is to use DFS with memoization to improve time complexity. 

 

We maintain a 2D array dp of size N*M. Since the increasing path can start from any point in the matrix, we use dp[i][j] to store the length of the longest increasing path starting from location (i, j). This ensures that we do not recalculate a visited location. 

 

  1. Now, we perform a DFS from each cell with the current cell as (x, y). We compare the cells in the four directions (dx, dy) and skip cells that are out of boundary or for which mat[x][y] > mat[dx][dy].
    1. If the cell (dx, dy) satisfies the conditions, we perform dfs from that cell and update maxLen as max of maxLen and 1 + (dfs from dx, dy).
  2. We then update dp[x][y] with maxLen.
  3. Finally, we return the maximum length of the increasing path as result.