Skip to content

Commit bbb5113

Browse files
committed
feat: add solutions to lc problem: No.1631
No.1631.Path With Minimum Effort
1 parent caafbda commit bbb5113

File tree

5 files changed

+248
-103
lines changed

5 files changed

+248
-103
lines changed

solution/1600-1699/1631.Path With Minimum Effort/README.md

+91
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ d[find(a)] = distance
127127

128128
二分枚举体力消耗值,用 BFS 找到满足条件的最小消耗值即可。
129129

130+
**方法三:堆优化版 Dijkstra 算法**
131+
130132
<!-- tabs:start -->
131133

132134
### **Python3**
@@ -185,6 +187,27 @@ class Solution:
185187
return left
186188
```
187189

190+
Dijkstra 算法:
191+
192+
```python
193+
class Solution:
194+
def minimumEffortPath(self, heights: List[List[int]]) -> int:
195+
INF = 0x3f3f3f3f
196+
m, n = len(heights), len(heights[0])
197+
dist = [[INF] * n for _ in range(m)]
198+
dist[0][0] = 0
199+
dirs = [-1, 0, 1, 0, -1]
200+
q = [(0, 0, 0)]
201+
while q:
202+
t, i, j = heappop(q)
203+
for k in range(4):
204+
x, y = i + dirs[k], j + dirs[k + 1]
205+
if 0 <= x < m and 0 <= y < n and max(t, abs(heights[x][y] - heights[i][j])) < dist[x][y]:
206+
dist[x][y] = max(t, abs(heights[x][y] - heights[i][j]))
207+
heappush(q, (dist[x][y], x, y))
208+
return dist[-1][-1]
209+
```
210+
188211
### **Java**
189212

190213
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -271,6 +294,40 @@ class Solution {
271294
}
272295
```
273296

297+
Dijkstra 算法:
298+
299+
```java
300+
class Solution {
301+
public int minimumEffortPath(int[][] heights) {
302+
int m = heights.length;
303+
int n = heights[0].length;
304+
int[][] dist = new int[m][n];
305+
for (int i = 0; i < m; ++i) {
306+
Arrays.fill(dist[i], 0x3f3f3f3f);
307+
}
308+
dist[0][0] = 0;
309+
PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
310+
q.offer(new int[]{0, 0, 0});
311+
int[] dirs = {-1, 0, 1, 0, -1};
312+
while (!q.isEmpty()) {
313+
int[] p = q.poll();
314+
int t = p[0], i = p[1], j = p[2];
315+
for (int k = 0; k < 4; ++k) {
316+
int x = i + dirs[k], y = j + dirs[k + 1];
317+
if (x >= 0 && x < m && y >= 0 && y < n) {
318+
int nd = Math.max(t, Math.abs(heights[x][y] - heights[i][j]));
319+
if (nd < dist[x][y]) {
320+
dist[x][y] = nd;
321+
q.offer(new int[]{nd, x, y});
322+
}
323+
}
324+
}
325+
}
326+
return dist[m - 1][n - 1];
327+
}
328+
}
329+
```
330+
274331
### **C++**
275332

276333
并查集:
@@ -348,6 +405,40 @@ public:
348405
};
349406
```
350407

408+
Dijkstra 算法:
409+
410+
```cpp
411+
class Solution {
412+
public:
413+
int minimumEffortPath(vector<vector<int>>& heights) {
414+
int m = heights.size(), n = heights[0].size();
415+
vector<vector<int>> dist(m, vector<int>(n, 0x3f3f3f3f));
416+
dist[0][0] = 0;
417+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
418+
q.push({0, 0, 0});
419+
vector<int> dirs = {-1, 0, 1, 0, -1};
420+
while (!q.empty())
421+
{
422+
auto [t, i, j] = q.top();
423+
q.pop();
424+
for (int k = 0; k < 4; ++k)
425+
{
426+
int x = i + dirs[k], y = j + dirs[k + 1];
427+
if (x >= 0 && x < m && y >= 0 && y < n)
428+
{
429+
int nd = max(t, abs(heights[x][y] - heights[i][j]));
430+
if (nd < dist[x][y])
431+
{
432+
dist[x][y] = nd;
433+
q.push({nd, x, y});
434+
}
435+
}
436+
}
437+
}
438+
return dist[m - 1][n - 1];
439+
}
440+
};
441+
```
351442
### **Go**
352443
353444
并查集:

solution/1600-1699/1631.Path With Minimum Effort/README_EN.md

+84
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ class Solution:
128128
return left
129129
```
130130

131+
```python
132+
class Solution:
133+
def minimumEffortPath(self, heights: List[List[int]]) -> int:
134+
INF = 0x3f3f3f3f
135+
m, n = len(heights), len(heights[0])
136+
dist = [[INF] * n for _ in range(m)]
137+
dist[0][0] = 0
138+
dirs = [-1, 0, 1, 0, -1]
139+
q = [(0, 0, 0)]
140+
while q:
141+
t, i, j = heappop(q)
142+
for k in range(4):
143+
x, y = i + dirs[k], j + dirs[k + 1]
144+
if 0 <= x < m and 0 <= y < n and max(t, abs(heights[x][y] - heights[i][j])) < dist[x][y]:
145+
dist[x][y] = max(t, abs(heights[x][y] - heights[i][j]))
146+
heappush(q, (dist[x][y], x, y))
147+
return dist[-1][-1]
148+
```
149+
131150
### **Java**
132151

133152
Union find:
@@ -212,6 +231,38 @@ class Solution {
212231
}
213232
```
214233

234+
```java
235+
class Solution {
236+
public int minimumEffortPath(int[][] heights) {
237+
int m = heights.length;
238+
int n = heights[0].length;
239+
int[][] dist = new int[m][n];
240+
for (int i = 0; i < m; ++i) {
241+
Arrays.fill(dist[i], 0x3f3f3f3f);
242+
}
243+
dist[0][0] = 0;
244+
PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
245+
q.offer(new int[]{0, 0, 0});
246+
int[] dirs = {-1, 0, 1, 0, -1};
247+
while (!q.isEmpty()) {
248+
int[] p = q.poll();
249+
int t = p[0], i = p[1], j = p[2];
250+
for (int k = 0; k < 4; ++k) {
251+
int x = i + dirs[k], y = j + dirs[k + 1];
252+
if (x >= 0 && x < m && y >= 0 && y < n) {
253+
int nd = Math.max(t, Math.abs(heights[x][y] - heights[i][j]));
254+
if (nd < dist[x][y]) {
255+
dist[x][y] = nd;
256+
q.offer(new int[]{nd, x, y});
257+
}
258+
}
259+
}
260+
}
261+
return dist[m - 1][n - 1];
262+
}
263+
}
264+
```
265+
215266
### **C++**
216267

217268
Union find:
@@ -289,6 +340,39 @@ public:
289340
};
290341
```
291342

343+
```cpp
344+
class Solution {
345+
public:
346+
int minimumEffortPath(vector<vector<int>>& heights) {
347+
int m = heights.size(), n = heights[0].size();
348+
vector<vector<int>> dist(m, vector<int>(n, 0x3f3f3f3f));
349+
dist[0][0] = 0;
350+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
351+
q.push({0, 0, 0});
352+
vector<int> dirs = {-1, 0, 1, 0, -1};
353+
while (!q.empty())
354+
{
355+
auto [t, i, j] = q.top();
356+
q.pop();
357+
for (int k = 0; k < 4; ++k)
358+
{
359+
int x = i + dirs[k], y = j + dirs[k + 1];
360+
if (x >= 0 && x < m && y >= 0 && y < n)
361+
{
362+
int nd = max(t, abs(heights[x][y] - heights[i][j]));
363+
if (nd < dist[x][y])
364+
{
365+
dist[x][y] = nd;
366+
q.push({nd, x, y});
367+
}
368+
}
369+
}
370+
}
371+
return dist[m - 1][n - 1];
372+
}
373+
};
374+
```
375+
292376
### **Go**
293377
294378
Union find:
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
class Solution {
2-
public:
3-
vector<int> p;
4-
5-
int minimumEffortPath(vector<vector<int>>& heights) {
6-
int m = heights.size(), n = heights[0].size();
7-
p.resize(m * n);
8-
for (int i = 0; i < p.size(); ++i) p[i] = i;
9-
vector<vector<int>> edges;
10-
for (int i = 0; i < m; ++i)
11-
{
12-
for (int j = 0; j < n; ++j)
13-
{
14-
if (i < m - 1) edges.push_back({abs(heights[i][j] - heights[i + 1][j]), i * n + j, (i + 1) * n + j});
15-
if (j < n - 1) edges.push_back({abs(heights[i][j] - heights[i][j + 1]), i * n + j, i * n + j + 1});
16-
}
17-
}
18-
sort(edges.begin(), edges.end());
19-
for (auto& e : edges)
20-
{
21-
int i = e[1], j = e[2];
22-
p[find(i)] = find(j);
23-
if (find(0) == find(m * n - 1)) return e[0];
24-
}
25-
return 0;
26-
}
27-
28-
int find(int x) {
29-
if (p[x] != x) p[x] = find(p[x]);
30-
return p[x];
31-
}
1+
class Solution {
2+
public:
3+
int minimumEffortPath(vector<vector<int>>& heights) {
4+
int m = heights.size(), n = heights[0].size();
5+
vector<vector<int>> dist(m, vector<int>(n, 0x3f3f3f3f));
6+
dist[0][0] = 0;
7+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
8+
q.push({0, 0, 0});
9+
vector<int> dirs = {-1, 0, 1, 0, -1};
10+
while (!q.empty())
11+
{
12+
auto [t, i, j] = q.top();
13+
q.pop();
14+
for (int k = 0; k < 4; ++k)
15+
{
16+
int x = i + dirs[k], y = j + dirs[k + 1];
17+
if (x >= 0 && x < m && y >= 0 && y < n)
18+
{
19+
int nd = max(t, abs(heights[x][y] - heights[i][j]));
20+
if (nd < dist[x][y])
21+
{
22+
dist[x][y] = nd;
23+
q.push({nd, x, y});
24+
}
25+
}
26+
}
27+
}
28+
return dist[m - 1][n - 1];
29+
}
3230
};
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
1-
class Solution {
2-
private int[] p;
3-
4-
public int minimumEffortPath(int[][] heights) {
5-
int m = heights.length;
6-
int n = heights[0].length;
7-
p = new int[m * n];
8-
for (int i = 0; i < p.length; ++i) {
9-
p[i] = i;
10-
}
11-
List<int[]> edges = new ArrayList<>();
12-
for (int i = 0; i < m; ++i) {
13-
for (int j = 0; j < n; ++j) {
14-
if (i < m - 1) {
15-
edges.add(new int[]{Math.abs(heights[i][j] - heights[i + 1][j]), i * n + j, (i + 1) * n + j});
16-
}
17-
if (j < n - 1) {
18-
edges.add(new int[]{Math.abs(heights[i][j] - heights[i][j + 1]), i * n + j, i * n + j + 1});
19-
}
20-
}
21-
}
22-
Collections.sort(edges, Comparator.comparingInt(a -> a[0]));
23-
for (int[] e : edges) {
24-
int i = e[1], j = e[2];
25-
p[find(i)] = find(j);
26-
if (find(0) == find(m * n - 1)) {
27-
return e[0];
28-
}
29-
}
30-
return 0;
31-
}
32-
33-
private int find(int x) {
34-
if (p[x] != x) {
35-
p[x] = find(p[x]);
36-
}
37-
return p[x];
38-
}
1+
class Solution {
2+
public int minimumEffortPath(int[][] heights) {
3+
int m = heights.length;
4+
int n = heights[0].length;
5+
int[][] dist = new int[m][n];
6+
for (int i = 0; i < m; ++i) {
7+
Arrays.fill(dist[i], 0x3f3f3f3f);
8+
}
9+
dist[0][0] = 0;
10+
PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
11+
q.offer(new int[]{0, 0, 0});
12+
int[] dirs = {-1, 0, 1, 0, -1};
13+
while (!q.isEmpty()) {
14+
int[] p = q.poll();
15+
int t = p[0], i = p[1], j = p[2];
16+
for (int k = 0; k < 4; ++k) {
17+
int x = i + dirs[k], y = j + dirs[k + 1];
18+
if (x >= 0 && x < m && y >= 0 && y < n) {
19+
int nd = Math.max(t, Math.abs(heights[x][y] - heights[i][j]));
20+
if (nd < dist[x][y]) {
21+
dist[x][y] = nd;
22+
q.offer(new int[]{nd, x, y});
23+
}
24+
}
25+
}
26+
}
27+
return dist[m - 1][n - 1];
28+
}
3929
}

0 commit comments

Comments
 (0)