Skip to content

Commit 319d6cf

Browse files
authored
Update 2577-minimum-time-to-visit-a-cell-in-a-grid.js
1 parent 5da730d commit 319d6cf

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

2577-minimum-time-to-visit-a-cell-in-a-grid.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,110 @@
1+
class PQ {
2+
constructor(comparator = (a, b) => a > b) {
3+
this.heap = []
4+
this.top = 0
5+
this.comparator = comparator
6+
}
7+
size() {
8+
return this.heap.length
9+
}
10+
isEmpty() {
11+
return this.size() === 0
12+
}
13+
peek() {
14+
return this.heap[this.top]
15+
}
16+
push(...values) {
17+
values.forEach((value) => {
18+
this.heap.push(value)
19+
this.siftUp()
20+
})
21+
return this.size()
22+
}
23+
pop() {
24+
const poppedValue = this.peek()
25+
const bottom = this.size() - 1
26+
if (bottom > this.top) {
27+
this.swap(this.top, bottom)
28+
}
29+
this.heap.pop()
30+
this.siftDown()
31+
return poppedValue
32+
}
33+
replace(value) {
34+
const replacedValue = this.peek()
35+
this.heap[this.top] = value
36+
this.siftDown()
37+
return replacedValue
38+
}
39+
40+
parent = (i) => ((i + 1) >>> 1) - 1
41+
left = (i) => (i << 1) + 1
42+
right = (i) => (i + 1) << 1
43+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
44+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
45+
siftUp = () => {
46+
let node = this.size() - 1
47+
while (node > this.top && this.greater(node, this.parent(node))) {
48+
this.swap(node, this.parent(node))
49+
node = this.parent(node)
50+
}
51+
}
52+
siftDown = () => {
53+
let node = this.top
54+
while (
55+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
56+
(this.right(node) < this.size() && this.greater(this.right(node), node))
57+
) {
58+
let maxChild =
59+
this.right(node) < this.size() &&
60+
this.greater(this.right(node), this.left(node))
61+
? this.right(node)
62+
: this.left(node)
63+
this.swap(node, maxChild)
64+
node = maxChild
65+
}
66+
}
67+
}
68+
69+
/**
70+
* @param {number[][]} grid
71+
* @return {number}
72+
*/
73+
const minimumTime = function (grid) {
74+
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;
75+
const dirs = [
76+
[-1, 0],
77+
[0, 1],
78+
[1, 0],
79+
[0, -1],
80+
]
81+
let m = grid.length,
82+
n = grid[0].length
83+
const visited = Array.from({ length: m }, () => Array(n).fill(false))
84+
const pq = new PQ((a, b) => a[0] < b[0])
85+
pq.push([grid[0][0], 0, 0])
86+
87+
while(!pq.isEmpty()) {
88+
const [v, r, c] = pq.pop()
89+
if(r === m - 1 && c === n - 1) return v
90+
if(visited[r][c]) continue
91+
visited[r][c] = true
92+
for(const [dr, dc] of dirs) {
93+
const nr = r + dr, nc = c + dc
94+
if(nr >= 0 && nr < m && nc >= 0 && nc < n && visited[nr][nc] === false) {
95+
const wait = (grid[nr][nc] - v) % 2 === 0 ? 1 : 0
96+
pq.push([Math.max(v + 1, grid[nr][nc] + wait), nr, nc])
97+
}
98+
}
99+
}
100+
101+
return -1
102+
}
103+
104+
105+
// another
106+
107+
1108
/**
2109
* @param {number[][]} grid
3110
* @return {number}

0 commit comments

Comments
 (0)