Skip to content

Commit 081b72c

Browse files
committed
329
1 parent 097b550 commit 081b72c

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

leetcode/329. Longest Increasing Path in a Matrix/README.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,39 @@ For each position `matrix[x][y]`,
3535
* otherwise, probe the 4 directions, `cnt[x][y]` is one greater than its largest neightbor.
3636

3737
```cpp
38-
// OJ: https://leetcode.com/problems/longest-increasing-path-in-a-matrix
38+
// OJ: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
3939
// Author: github.com/lzl124631x
4040
// Time: O(MN)
4141
// Space: O(MN)
4242
class Solution {
43-
private:
44-
int M, N, ans = 0, dirs[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
45-
vector<vector<int>> cnt;
46-
int dfs(vector<vector<int>> &matrix, int x, int y) {
47-
if (cnt[x][y]) return cnt[x][y];
48-
cnt[x][y] = 1;
49-
for (auto dir : dirs) {
50-
int i = x + dir[0], j = y + dir[1];
51-
if (i < 0 || i >= M || j < 0 || j >= N || matrix[i][j] <= matrix[x][y]) continue;
52-
cnt[x][y] = max(cnt[x][y], 1 + dfs(matrix, i, j));
43+
vector<vector<int>> cnt;
44+
int ans = 0, M, N, dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
45+
int dfs(vector<vector<int>> &A, int x, int y) {
46+
if (cnt[x][y]) return cnt[x][y];
47+
cnt[x][y] = 1;
48+
for (auto &dir : dirs) {
49+
int a = x + dir[0], b = y + dir[1];
50+
if (a < 0 || b < 0 || a >= M || b >= N || A[a][b] <= A[x][y]) continue;
51+
cnt[x][y] = max(cnt[x][y], 1 + dfs(A, a, b));
52+
}
53+
return cnt[x][y];
5354
}
54-
ans = max(ans, cnt[x][y]);
55-
return cnt[x][y];
56-
}
5755
public:
58-
int longestIncreasingPath(vector<vector<int>>& matrix) {
59-
if (matrix.empty() || matrix[0].empty()) return 0;
60-
M = matrix.size(), N = matrix[0].size();
61-
cnt = vector<vector<int>> (M, vector<int>(N, 0));
62-
for (int i = 0; i < M; ++i)
63-
for (int j = 0; j < N; ++j)
64-
dfs(matrix, i, j);
65-
return ans;
66-
}
56+
int longestIncreasingPath(vector<vector<int>>& A) {
57+
if (A.empty() || A[0].empty()) return 0;
58+
M = A.size(), N = A[0].size();
59+
cnt.assign(M, vector<int>(N));
60+
for (int i = 0; i < M; ++i)
61+
for (int j = 0; j < N; ++j)
62+
ans = max(ans, dfs(A, i, j));
63+
return ans;
64+
}
6765
};
6866
```
6967
70-
## Solution 2. Topological Sort
68+
## Solution 2. Topological Sort (BFS)
69+
70+
Note that the DFS version of topological sort solution is the same as the Solution 1.
7171
7272
```cpp
7373
// OJ: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
// OJ: https://leetcode.com/problems/longest-increasing-path-in-a-matrix
1+
// OJ: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
22
// Author: github.com/lzl124631x
33
// Time: O(MN)
44
// Space: O(MN)
55
class Solution {
6-
private:
7-
int M, N, ans = 0, dirs[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
8-
vector<vector<int>> cnt;
9-
int dfs(vector<vector<int>> &matrix, int x, int y) {
10-
if (cnt[x][y]) return cnt[x][y];
11-
cnt[x][y] = 1;
12-
for (auto dir : dirs) {
13-
int i = x + dir[0], j = y + dir[1];
14-
if (i < 0 || i >= M || j < 0 || j >= N || matrix[i][j] <= matrix[x][y]) continue;
15-
cnt[x][y] = max(cnt[x][y], 1 + dfs(matrix, i, j));
6+
vector<vector<int>> cnt;
7+
int ans = 0, M, N, dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
8+
int dfs(vector<vector<int>> &A, int x, int y) {
9+
if (cnt[x][y]) return cnt[x][y];
10+
cnt[x][y] = 1;
11+
for (auto &dir : dirs) {
12+
int a = x + dir[0], b = y + dir[1];
13+
if (a < 0 || b < 0 || a >= M || b >= N || A[a][b] <= A[x][y]) continue;
14+
cnt[x][y] = max(cnt[x][y], 1 + dfs(A, a, b));
15+
}
16+
return cnt[x][y];
1617
}
17-
ans = max(ans, cnt[x][y]);
18-
return cnt[x][y];
19-
}
2018
public:
21-
int longestIncreasingPath(vector<vector<int>>& matrix) {
22-
if (matrix.empty() || matrix[0].empty()) return 0;
23-
M = matrix.size(), N = matrix[0].size();
24-
cnt = vector<vector<int>> (M, vector<int>(N, 0));
25-
for (int i = 0; i < M; ++i)
26-
for (int j = 0; j < N; ++j)
27-
dfs(matrix, i, j);
28-
return ans;
29-
}
19+
int longestIncreasingPath(vector<vector<int>>& A) {
20+
if (A.empty() || A[0].empty()) return 0;
21+
M = A.size(), N = A[0].size();
22+
cnt.assign(M, vector<int>(N));
23+
for (int i = 0; i < M; ++i)
24+
for (int j = 0; j < N; ++j)
25+
ans = max(ans, dfs(A, i, j));
26+
return ans;
27+
}
3028
};

0 commit comments

Comments
 (0)