-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest Increasing Path in a Matrix.cpp
54 lines (44 loc) · 1.33 KB
/
Longest Increasing Path in a Matrix.cpp
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
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
int row = matrix.size();
if(row == 0)
return 0;
int col = matrix[0].size();
vector<vector<int>> path(row, vector<int>(col, -1));
int maxlen = 0;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++){
int val = dfs(path, matrix, i, j, row, col);
maxlen = max(maxlen, val);
}
return maxlen;
}
int dfs(vector<vector<int>>& path, vector<vector<int>>& matrix, int i , int j, int row, int col){
if(path[i][j] == -1){
path[i][j] = 1;
if(i > 0){
if(matrix[i][j] > matrix[i-1][j]){
path[i][j] = max(path[i][j], dfs(path, matrix, i-1, j, row, col) + 1);
}
}
if(j > 0 ){
if(matrix[i][j] > matrix[i][j-1]){
path[i][j] = max(path[i][j], dfs(path, matrix, i, j-1, row, col) + 1);
}
}
if(i < row - 1 ){
if(matrix[i][j] > matrix[i+1][j]){
path[i][j] = max(path[i][j], dfs(path, matrix, i+1, j, row, col) + 1);
}
}
if(j < col -1){
if(matrix[i][j] > matrix[i][j+1]){
path[i][j] = max(path[i][j], dfs(path, matrix, i, j+1, row, col) + 1);
}
}
}
// cout << i <<" "<< j<<" "<<path[i][j]<<endl;
return path[i][j];
}
};