Longest Increasing Path In A 2D Matrix

Hard
0/120
Average time to solve is 10m
profile
Contributed by
16 upvotes
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.
Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1 :
1
3 3 
1 2 3 
5 6 4
9 5 11 
Sample Output 1 :
5

Sample Output 1 Explanation:

The longest increasing path in the MATRIX is 1 → 2 → 3 → 4 → 11 which is of length 5.
Sample Input 2 :
1
3 3
8 8 3
5 5 7
2 1 1
Sample Output 2 :
4

Sample Output 2 Explanation :

The longest increasing path in the MATRIX is 1 → 2 → 5 → 8 which is of length 4.
Hint

Naively find length of increasing path from each cell.

Approaches (2)
DFS

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.
Time Complexity

O(4^(N*M)) where N and M denote the number of rows and columns, respectively.

 

We perform a DFS for every element. In the worst case, when elements in rows and columns are sorted in ascending order, we move from each cell in the matrix to all 4 neighbouring larger elements, and then from each of those elements to all of their neighbouring larger elements.

Therefore, the overall time complexity is O(4^(N*M)).

Space Complexity

O(N*M), where N and M denote the number of rows and columns, respectively.

 

O(H) stack space is used in each depth first search where H is the maximum depth of the recursion. In the worst case, H = N*M.

Code Solution
(100% EXP penalty)
Longest Increasing Path In A 2D Matrix
Full screen
Console