-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath-with-minimum-effort.js
74 lines (56 loc) · 1.47 KB
/
path-with-minimum-effort.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Cell {
constructor(i, j, effort) {
this.i = i;
this.j = j;
this.effort = effort;
}
}
/**
* @param {number[][]} heights
* @return {number}
*/
function minimumEffortPath(heights) {
const h = heights.length;
const w = heights[0].length;
const directions = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const efforts = new Array(h).fill(null).map(() => new Array(w).fill(Number.MAX_SAFE_INTEGER));
const queue = new MinPriorityQueue({ priority: (bid) => bid.effort });
const visited = new Array(h).fill(null).map(() => new Array(w).fill(false));
let i = 0;
let j = 0;
efforts[i][j] = 0;
queue.enqueue(new Cell(0, 0, 0));
while (!queue.isEmpty()) {
const next = queue.dequeue().element;
const i = next.i;
const j = next.j;
if (visited[i][j]) {
continue;
}
if (i == h - 1 && j == w - 1) {
return next.effort;
}
visited[i][j] = true;
let current = heights[i][j];
let currentEffort = efforts[i][j];
for (let dir of directions) {
let newi = i + dir[0];
let newj = j + dir[1];
if (newi < 0 || newj < 0 || newi == h || newj == w) {
continue;
}
let diff = Math.abs(current - heights[newi][newj]);
let maxEffort = Math.max(diff, currentEffort);
if (maxEffort < efforts[newi][newj]) {
efforts[newi][newj] = maxEffort;
queue.enqueue(new Cell(newi, newj, maxEffort));
}
}
}
return efforts[h - 1][w - 1];
}