forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
longest-increasing-path-in-a-matrix.cpp
46 lines (40 loc) · 1.41 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
// Time: O(m * n)
// Space: O(m * n)
// DFS + Memorization solution.
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
if (matrix.empty()) {
return 0;
}
int res = 0;
vector<vector<int>> max_lengths(matrix.size(), vector<int>(matrix[0].size()));
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[0].size(); ++j) {
res = max(res, longestpath(matrix, i, j, &max_lengths));
}
}
return res;
}
private:
int longestpath(const vector<vector<int>>& matrix, const int i, const int j,
vector<vector<int>> *max_lengths) {
if ((*max_lengths)[i][j] > 0) {
return (*max_lengths)[i][j];
}
int max_depth = 0;
const vector<pair<int, int>> directions{{0, -1}, {0, 1},
{-1, 0}, {1, 0}};
for (const auto& d : directions) {
const int x = i + d.first, y = j + d.second;
if (x >= 0 && x < matrix.size() &&
y >= 0 && y < matrix[0].size() &&
matrix[x][y] < matrix[i][j]) {
max_depth = max(max_depth,
longestpath(matrix, x, y, max_lengths));
}
}
(*max_lengths)[i][j] = max_depth + 1;
return (*max_lengths)[i][j];
}
};