-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path329.cpp
66 lines (66 loc) · 1.83 KB
/
329.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
55
56
57
58
59
60
61
62
63
64
65
66
// dp.cpp
class Solution {
public:
int row;
int col;
vector<vector<int>> dp;
int longestIncreasingPath(vector<vector<int>> &matrix) {
row = matrix.size();
if (row == 0)
return 0;
col = matrix[0].size();
dp.resize(row);
for (int i = 0; i < row; i++)
dp[i].resize(col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
dp[i][j] = 0;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
calc(i, j, matrix);
int max = INT_MIN;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (dp[i][j] > max)
max = dp[i][j];
return max;
}
int calc(int i, int j, vector<vector<int>> &matrix) {
int max = 1;
int temp;
if (i - 1 >= 0)
if (matrix[i][j] > matrix[i - 1][j])
if (dp[i - 1][j] > 0)
max = dp[i - 1][j] + 1 > max ? dp[i - 1][j] + 1 : max;
else {
temp = calc(i - 1, j, matrix) + 1;
max = temp > max ? temp : max;
}
if (i + 1 < row)
if (matrix[i][j] > matrix[i + 1][j])
if (dp[i + 1][j] > 0)
max = dp[i + 1][j] + 1 > max ? dp[i + 1][j] + 1 : max;
else {
temp = calc(i + 1, j, matrix) + 1;
max = temp > max ? temp : max;
}
if (j - 1 >= 0)
if (matrix[i][j] > matrix[i][j - 1])
if (dp[i][j - 1] > 0)
max = dp[i][j - 1] + 1 > max ? dp[i][j - 1] + 1 : max;
else {
temp = calc(i, j - 1, matrix) + 1;
max = temp > max ? temp : max;
}
if (j + 1 < col)
if (matrix[i][j] > matrix[i][j + 1])
if (dp[i][j + 1] > 0)
max = dp[i][j + 1] + 1 > max ? dp[i][j + 1] + 1 : max;
else {
temp = calc(i, j + 1, matrix) + 1;
max = temp > max ? temp : max;
}
dp[i][j] = max;
return max;
}
};