forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest_Increasing_Path_in_a_Matrix.cpp
43 lines (40 loc) · 1.37 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
class Solution {
public:
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
bool isValid(int x, int y, int newX, int newY, vector<vector<int>>& matrix) {
return (newX >= 0 and newY >= 0 and newX < matrix.size() and newY < matrix[0].size() and matrix[newX][newY] > matrix[x][y]);
}
int longestIncreasingPathUtil(int x, int y, vector<vector<int>>& matrix, vector<vector<int>>& dp) {
if(dp[x][y] > 0) {
return dp[x][y];
}
int maxLen = 0;
for(int i = 0, n = sizeof(dx) / sizeof(dx[0]); i < n; ++i) {
int newX = x + dx[i];
int newY = y + dy[i];
if(isValid(x, y, newX, newY, matrix)) {
maxLen = max(maxLen, longestIncreasingPathUtil(newX, newY, matrix, dp));
}
}
return dp[x][y] = 1 + maxLen;
}
int longestIncreasingPath(vector<vector<int>>& matrix) {
int m = matrix.size();
if(m == 0) {
return 0;
}
int n = matrix[0].size();
if(n == 0) {
return 0;
}
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
int maxLen = INT_MIN;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
maxLen = max(maxLen, longestIncreasingPathUtil(i, j, matrix, dp));
}
}
return maxLen;
}
};