Skip to content

Commit 102d709

Browse files
authored
Update 2812-find-the-safest-path-in-a-grid.js
1 parent d1b9ab8 commit 102d709

File tree

1 file changed

+44
-108
lines changed

1 file changed

+44
-108
lines changed

2812-find-the-safest-path-in-a-grid.js

Lines changed: 44 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,124 +3,60 @@
33
* @return {number}
44
*/
55
const maximumSafenessFactor = function (grid) {
6-
let n = grid.length,
7-
m = grid[0].length,
8-
ans = Infinity
9-
const { min, abs } = Math
10-
let t = []
11-
for (let i = 0; i < n; i++) {
12-
for (let j = 0; j < m; j++) {
13-
if (grid[i][j] == 1) {
14-
t.push([i, j]) // keeping track of each thief
6+
const n = grid.length;
7+
const isInBound = (r, c) => r >= 0 && r < n && c >= 0 && c < n;
8+
const dist = new Array(n).fill(0).map(() => new Array(n).fill(Infinity));
9+
const queue = [];
10+
11+
for (let r = 0; r < n; r++) {
12+
for (let c = 0; c < n; c++) {
13+
if (grid[r][c] === 1) {
14+
dist[r][c] = 0;
15+
queue.push([r, c]);
1516
}
1617
}
1718
}
1819

19-
const vis = Array.from({ length: n }, () => Array(m).fill(0))
20-
21-
const pq = new PQ((a, b) => a[0] > b[0])
22-
let m_dist = Infinity
23-
for (const thieve of t) {
24-
m_dist = Math.min(m_dist, thieve[0] + thieve[1]) // Calculating Manhattan distance between current cell and all thieves
25-
}
26-
let dr = [0, -1, 0, 1],
27-
dc = [-1, 0, 1, 0]
28-
pq.push([m_dist, [0, 0]])
29-
vis[0][0] = 1
30-
// int mn_dist = 0;
31-
while (!pq.isEmpty()) {
32-
let temp = pq.pop()
33-
34-
let dist = temp[0],
35-
r = temp[1][0],
36-
c = temp[1][1]
37-
// mn_dist = min(dist,mn_dist);
38-
if (r == n - 1 && c == m - 1) {
39-
return dist // return path safety when end is reached
40-
}
41-
for (let i = 0; i < 4; i++) {
42-
let nr = r + dr[i]
43-
let nc = c + dc[i]
44-
if (nr >= 0 && nc >= 0 && nr < n && nc < m && !vis[nr][nc]) {
45-
//for every adjacent cell calculate the minimum mahattan distance betwwen cell and thieves.
46-
vis[nr][nc] = 1
47-
let m_dist = Infinity
48-
for (let thieve of t) {
49-
m_dist = min(m_dist, abs(thieve[0] - nr) + abs(thieve[1] - nc))
50-
}
20+
while (queue.length) {
21+
const [r, c] = queue.shift();
22+
const neighbors = [
23+
[r + 1, c],
24+
[r - 1, c],
25+
[r, c + 1],
26+
[r, c - 1],
27+
];
5128

52-
// push the minimum of current distance and the minimum distance of the path till now
53-
pq.push([min(m_dist, dist), [nr, nc]])
29+
for (const [nr, nc] of neighbors) {
30+
if (isInBound(nr, nc) && dist[nr][nc] === Infinity) {
31+
dist[nr][nc] = dist[r][c] + 1;
32+
queue.push([nr, nc]);
5433
}
5534
}
5635
}
57-
return ans
58-
}
5936

60-
class PQ {
61-
constructor(comparator = (a, b) => a > b) {
62-
this.heap = []
63-
this.top = 0
64-
this.comparator = comparator
65-
}
66-
size() {
67-
return this.heap.length
68-
}
69-
isEmpty() {
70-
return this.size() === 0
71-
}
72-
peek() {
73-
return this.heap[this.top]
74-
}
75-
push(...values) {
76-
values.forEach((value) => {
77-
this.heap.push(value)
78-
this.siftUp()
79-
})
80-
return this.size()
81-
}
82-
pop() {
83-
const poppedValue = this.peek()
84-
const bottom = this.size() - 1
85-
if (bottom > this.top) {
86-
this.swap(this.top, bottom)
87-
}
88-
this.heap.pop()
89-
this.siftDown()
90-
return poppedValue
91-
}
92-
replace(value) {
93-
const replacedValue = this.peek()
94-
this.heap[this.top] = value
95-
this.siftDown()
96-
return replacedValue
97-
}
37+
const maxDistance = new Array(n).fill(0).map(() => new Array(n).fill(0));
38+
maxDistance[0][0] = dist[0][0];
39+
queue.push([0, 0]);
9840

99-
parent = (i) => ((i + 1) >>> 1) - 1
100-
left = (i) => (i << 1) + 1
101-
right = (i) => (i + 1) << 1
102-
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
103-
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
104-
siftUp = () => {
105-
let node = this.size() - 1
106-
while (node > this.top && this.greater(node, this.parent(node))) {
107-
this.swap(node, this.parent(node))
108-
node = this.parent(node)
109-
}
110-
}
111-
siftDown = () => {
112-
let node = this.top
113-
while (
114-
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
115-
(this.right(node) < this.size() && this.greater(this.right(node), node))
116-
) {
117-
let maxChild =
118-
this.right(node) < this.size() &&
119-
this.greater(this.right(node), this.left(node))
120-
? this.right(node)
121-
: this.left(node)
122-
this.swap(node, maxChild)
123-
node = maxChild
41+
while (queue.length) {
42+
const [r, c] = queue.shift();
43+
const neighbors = [
44+
[r + 1, c],
45+
[r - 1, c],
46+
[r, c + 1],
47+
[r, c - 1],
48+
];
49+
50+
for (const [nr, nc] of neighbors) {
51+
if (isInBound(nr, nc)) {
52+
const newDistance = Math.min(maxDistance[r][c], dist[nr][nc]);
53+
if (newDistance > maxDistance[nr][nc]) {
54+
maxDistance[nr][nc] = newDistance;
55+
queue.push([nr, nc]);
56+
}
57+
}
12458
}
12559
}
60+
61+
return maxDistance[n - 1][n - 1];
12662
}

0 commit comments

Comments
 (0)