-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLongestPathInMatrix.cpp
More file actions
64 lines (56 loc) · 1.74 KB
/
Copy pathLongestPathInMatrix.cpp
File metadata and controls
64 lines (56 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Leetcode - Hard
Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Input: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
]
Output: 4
*/
int _X[] = {1, -1, 0, 0} ;
int _Y[] = {0, 0, 1, -1} ;
bool inside(int &i, int &j, int &r, int &c){
return i >= 0 && j >= 0 && i < r && j < c ;
}
int dfs(int& r, int& c, vector<vector<int>> &matrix, vector<vector<bool>> &vis, vector<vector<int>> &dp, int i, int j){
if( vis[i][j] ) return dp[i][j] ;
vis[i][j] = true ;
int ans = -1 ;
for(int m = 0 ; m < 4 ; m++){
int nx = i + _X[m], ny = j + _Y[m] ;
if( !inside(nx, ny, r, c) ) continue ;
if( matrix[nx][ny] <= matrix[i][j] ) continue ;
if( !vis[nx][ny] )
ans = max(ans, dfs(r, c, matrix, vis, dp, nx, ny) ) ;
else
ans = max(ans, dp[nx][ny]) ;
}
return ( dp[i][j] = ans + 1 );
}
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
int r = matrix.size();
if( r == 0 ) return 0 ;
int c = matrix[0].size() ;
vector<vector<bool>> vis(r, vector<bool> (c, false) ) ;
vector<vector<int>> dp(r, vector<int>(c, 0)) ;
for(int i = 0 ; i < r ; i++){
for(int j = 0 ; j < c ; j++){
if( !vis[i][j] ){
dfs(r, c, matrix, vis, dp, i, j) ;
}
}
}
int ans = -1 ;
for(int i = 0 ; i < r ; i++){
for(int j = 0 ; j < c ; j++){
ans = max(ans, dp[i][j]) ;
}
}
return ans + 1 ;
}
};