Longest Increasing Path in a Matrix
Hard Accuracy: 62.9% Submissions: 1136 Points: 8

Given a matrix with n rows and m columns. Your task is to find the length of the longest increasing path in matrix, here increasing path means that the value in the specified path increases. For example if a path of length k has values a1, a2, a3, .... ak  , then for every i from [2,k] this condition must hold a> ai-1.  No cell should be revisited in the path.
From each cell, you can either move in four directions: left, right, up, or down. You are not allowed to move diagonally or move outside the boundary.

Example 1:

Input:
n = 3, m = 3
matrix[][] = {{1 2 3},
{4 5 6},
{7 8 9}}
Output:
5
Explanation:
The longest increasing path is
{1, 2, 3, 6, 9}.


Example 2:

Input:
n = 3, m = 3
matrix[][] = {{3 4 5},
{6 2 6},
{2 2 1}}
Output:
4
Explanation:
The longest increasing path is
{3, 4, 5, 6}.

You only need to implement the given function longestIncreasingPath() which takes the two integers and and the matrix matrix as input parameters, and returns the length of the longest increasing path in matrix.

Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)

Constraints:
1 ≤
n,m ≤ 1000
0 ≤
matrix[i] ≤ 230

We are replacing the old Disqus forum with the new Discussions section given below.

### Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

#### My Submissions:  