Skip to content

Commit a06b518

Browse files
authored
Update 1631-path-with-minimum-effort.js
1 parent a153c44 commit a06b518

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

1631-path-with-minimum-effort.js

+100
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,103 @@ const minimumEffortPath = function (heights) {
4848
return false
4949
}
5050
}
51+
52+
// another
53+
54+
/**
55+
* @param {number[][]} heights
56+
* @return {number}
57+
*/
58+
const minimumEffortPath = function(heights) {
59+
const rows = heights.length
60+
const cols = heights[0].length
61+
const dirs = [[-1, 0], [1, 0], [0, 1], [0, -1]]
62+
const dist = Array.from({ length: rows }, () => Array(cols).fill(Infinity))
63+
const pq = new PriorityQueue()
64+
pq.push([0, 0, 0])
65+
dist[0][0] = 0
66+
while(pq.size) {
67+
const cur = pq.pop()
68+
if(cur[1] === rows - 1 && cur[2] === cols - 1) return cur[0]
69+
for(let dir of dirs) {
70+
const nr = cur[1] + dir[0]
71+
const nc = cur[2] + dir[1]
72+
if(nr < 0 || nr >= rows || nc < 0 || nc >= cols) continue
73+
const diff = Math.max(cur[0], Math.abs(heights[nr][nc] - heights[cur[1]][cur[2]]))
74+
if(dist[nr][nc] > diff) {
75+
dist[nr][nc] = diff
76+
pq.push([diff, nr, nc])
77+
}
78+
}
79+
}
80+
return 0
81+
};
82+
83+
class PriorityQueue {
84+
constructor(comparator = (a, b) => a[0] < b[0]) {
85+
this.heap = []
86+
this.top = 0
87+
this.comparator = comparator
88+
}
89+
size() {
90+
return this.heap.length
91+
}
92+
isEmpty() {
93+
return this.size() === 0
94+
}
95+
peek() {
96+
return this.heap[this.top]
97+
}
98+
push(...values) {
99+
values.forEach((value) => {
100+
this.heap.push(value)
101+
this.siftUp()
102+
})
103+
return this.size()
104+
}
105+
pop() {
106+
const poppedValue = this.peek()
107+
const bottom = this.size() - 1
108+
if (bottom > this.top) {
109+
this.swap(this.top, bottom)
110+
}
111+
this.heap.pop()
112+
this.siftDown()
113+
return poppedValue
114+
}
115+
replace(value) {
116+
const replacedValue = this.peek()
117+
this.heap[this.top] = value
118+
this.siftDown()
119+
return replacedValue
120+
}
121+
122+
parent = (i) => ((i + 1) >>> 1) - 1
123+
left = (i) => (i << 1) + 1
124+
right = (i) => (i + 1) << 1
125+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
126+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
127+
siftUp = () => {
128+
let node = this.size() - 1
129+
while (node > this.top && this.greater(node, this.parent(node))) {
130+
this.swap(node, this.parent(node))
131+
node = this.parent(node)
132+
}
133+
}
134+
siftDown = () => {
135+
let node = this.top
136+
while (
137+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
138+
(this.right(node) < this.size() && this.greater(this.right(node), node))
139+
) {
140+
let maxChild =
141+
this.right(node) < this.size() &&
142+
this.greater(this.right(node), this.left(node))
143+
? this.right(node)
144+
: this.left(node)
145+
this.swap(node, maxChild)
146+
node = maxChild
147+
}
148+
}
149+
}
150+

0 commit comments

Comments
 (0)