forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath With Minimum Effort.java
26 lines (26 loc) · 1.01 KB
/
Path With Minimum Effort.java
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
// 36 ms. 97.51%
class Solution {
private static final int[][] dir = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
public int minimumEffortPath(int[][] heights) {
int m = heights.length, n = heights[0].length;
int[][] dist = new int[m][n];
for(int i = 0; i < m; i++) Arrays.fill(dist[i], Integer.MAX_VALUE);
dist[0][0] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);
pq.add(new int[] {0, 0, 0});
while(!pq.isEmpty()) {
int[] p = pq.poll();
int i = p[0], j = p[1];
if(i == m - 1 && j == n - 1) break;
for(int[] d: dir) {
int x = i + d[0], y = j + d[1];
if(x < 0 || x >= m || y < 0 || y >= n) continue;
int alt = Math.max(p[2], Math.abs(heights[i][j] - heights[x][y]));
if(alt < dist[x][y]) {
pq.add(new int[] {x, y, dist[x][y] = alt});
}
}
}
return dist[m - 1][n - 1];
}
}