Skip to content

Commit 097b550

Browse files
committed
329
1 parent 1362a6f commit 097b550

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Return `4`
2727
The longest increasing path is `[3, 4, 5, 6]`. Moving diagonally is not allowed.
2828

2929
# Solution 1. DFS
30+
3031
Typical DFS. Use a `vector<vector<int>> cnt`, where `cnt[i][j]` is the length of longest increasing path starting from `matrix[i][j]`. Initially values in `cnt` are all zeroes.
3132

3233
For each position `matrix[x][y]`,
@@ -64,4 +65,51 @@ public:
6465
return ans;
6566
}
6667
};
68+
```
69+
70+
## Solution 2. Topological Sort
71+
72+
```cpp
73+
// OJ: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
74+
// Author: github.com/lzl124631x
75+
// Time: O(MN)
76+
// Space: O(MN)
77+
// Ref: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/discuss/288520/BFS-Implemented-Topological-Sort
78+
class Solution {
79+
public:
80+
int longestIncreasingPath(vector<vector<int>>& A) {
81+
if (A.empty() || A[0].empty()) return 0;
82+
int M = A.size(), N = A[0].size(), ans = 0, dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
83+
vector<vector<int>> indegree(M, vector<int>(N));
84+
for (int i = 0; i < M; ++i) {
85+
for (int j = 0; j < N; ++j) {
86+
for (auto &dir : dirs) {
87+
int x = i + dir[0], y = j + dir[1];
88+
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] <= A[i][j]) continue;
89+
indegree[x][y]++;
90+
}
91+
}
92+
}
93+
queue<vector<int>> q;
94+
for (int i = 0; i < M; ++i) {
95+
for (int j = 0; j < N; ++j) {
96+
if (indegree[i][j] == 0) q.push({ i, j });
97+
}
98+
}
99+
while (q.size()) {
100+
int sz = q.size();
101+
while (sz--) {
102+
int x = q.front()[0], y = q.front()[1];
103+
q.pop();
104+
for (auto &dir : dirs) {
105+
int a = x + dir[0], b = y + dir[1];
106+
if (a < 0 || a >= M || b < 0 || b >= N || A[a][b] <= A[x][y]) continue;
107+
if (--indegree[a][b] == 0) q.push({ a, b });
108+
}
109+
}
110+
++ans;
111+
}
112+
return ans;
113+
}
114+
};
67115
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// OJ: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
2+
// Author: github.com/lzl124631x
3+
// Time: O(MN)
4+
// Space: O(MN)
5+
// Ref: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/discuss/288520/BFS-Implemented-Topological-Sort
6+
class Solution {
7+
public:
8+
int longestIncreasingPath(vector<vector<int>>& A) {
9+
if (A.empty() || A[0].empty()) return 0;
10+
int M = A.size(), N = A[0].size(), ans = 0, dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
11+
vector<vector<int>> indegree(M, vector<int>(N));
12+
for (int i = 0; i < M; ++i) {
13+
for (int j = 0; j < N; ++j) {
14+
for (auto &dir : dirs) {
15+
int x = i + dir[0], y = j + dir[1];
16+
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] <= A[i][j]) continue;
17+
indegree[x][y]++;
18+
}
19+
}
20+
}
21+
queue<vector<int>> q;
22+
for (int i = 0; i < M; ++i) {
23+
for (int j = 0; j < N; ++j) {
24+
if (indegree[i][j] == 0) q.push({ i, j });
25+
}
26+
}
27+
while (q.size()) {
28+
int sz = q.size();
29+
while (sz--) {
30+
int x = q.front()[0], y = q.front()[1];
31+
q.pop();
32+
for (auto &dir : dirs) {
33+
int a = x + dir[0], b = y + dir[1];
34+
if (a < 0 || a >= M || b < 0 || b >= N || A[a][b] <= A[x][y]) continue;
35+
if (--indegree[a][b] == 0) q.push({ a, b });
36+
}
37+
}
38+
++ans;
39+
}
40+
return ans;
41+
}
42+
};

0 commit comments

Comments
 (0)