-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPathWithMinimumEffort.java
39 lines (31 loc) · 1.36 KB
/
PathWithMinimumEffort.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
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public static int MinimumEffort(int rows, int columns, int[][] heights) {
int[][] effort = new int[rows][columns];
for (int i = 0; i < rows; i++) {
Arrays.fill(effort[i], Integer.MAX_VALUE);
}
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);
pq.offer(new int[]{0, 0, 0}); // (row, col, effort)
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while (!pq.isEmpty()) {
int[] curr = pq.poll();
int row = curr[0];
int col = curr[1];
int currEffort = curr[2];
if (row == rows - 1 && col == columns - 1)
return currEffort;
for (int[] dir : directions) {
int newRow = row + dir[0];
int newCol = col + dir[1];
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < columns) {
int newEffort = Math.max(currEffort, Math.abs(heights[newRow][newCol] - heights[row][col]));
if (newEffort < effort[newRow][newCol]) {
effort[newRow][newCol] = newEffort;
pq.offer(new int[]{newRow, newCol, newEffort});
}
}
}
}
return -1; // Unreachable
}
}