Skip to content

Commit ffd082d

Browse files
authored
feat: add solutions to lc problem: No.2577 (#3824)
1 parent 8cbbd41 commit ffd082d

File tree

4 files changed

+201
-1
lines changed

4 files changed

+201
-1
lines changed

solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,76 @@ func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
253253
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
254254
```
255255

256+
#### TypeScript
257+
258+
```ts
259+
function minimumTime(grid: number[][]): number {
260+
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;
261+
262+
const [m, n] = [grid.length, grid[0].length];
263+
const DIRS = [-1, 0, 1, 0, -1];
264+
const q = new MinPriorityQueue({ priority: ([x]) => x });
265+
const dist: number[][] = Array.from({ length: m }, () =>
266+
new Array(n).fill(Number.POSITIVE_INFINITY),
267+
);
268+
dist[0][0] = 0;
269+
q.enqueue([0, 0, 0]);
270+
271+
while (true) {
272+
const [t, i, j] = q.dequeue().element;
273+
if (i === m - 1 && j === n - 1) return t;
274+
275+
for (let k = 0; k < 4; k++) {
276+
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
277+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
278+
279+
let nt = t + 1;
280+
if (nt < grid[x][y]) {
281+
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
282+
}
283+
if (nt < dist[x][y]) {
284+
dist[x][y] = nt;
285+
q.enqueue([nt, x, y]);
286+
}
287+
}
288+
}
289+
}
290+
```
291+
292+
#### JavaScript
293+
294+
```js
295+
function minimumTime(grid) {
296+
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;
297+
298+
const [m, n] = [grid.length, grid[0].length];
299+
const DIRS = [-1, 0, 1, 0, -1];
300+
const q = new MinPriorityQueue({ priority: ([x]) => x });
301+
const dist = Array.from({ length: m }, () => new Array(n).fill(Number.POSITIVE_INFINITY));
302+
dist[0][0] = 0;
303+
q.enqueue([0, 0, 0]);
304+
305+
while (true) {
306+
const [t, i, j] = q.dequeue().element;
307+
if (i === m - 1 && j === n - 1) return t;
308+
309+
for (let k = 0; k < 4; k++) {
310+
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
311+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
312+
313+
let nt = t + 1;
314+
if (nt < grid[x][y]) {
315+
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
316+
}
317+
if (nt < dist[x][y]) {
318+
dist[x][y] = nt;
319+
q.enqueue([nt, x, y]);
320+
}
321+
}
322+
}
323+
}
324+
```
325+
256326
<!-- tabs:end -->
257327

258328
<!-- solution:end -->

solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README_EN.md

+71-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ The final time is 7. It can be shown that it is the minimum time possible.
7272
</ul>
7373

7474
<p>&nbsp;</p>
75-
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
75+
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
7676
}
7777
.spoiler {overflow:hidden;}
7878
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
@@ -260,6 +260,76 @@ func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
260260
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
261261
```
262262

263+
#### TypeScript
264+
265+
```ts
266+
function minimumTime(grid: number[][]): number {
267+
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;
268+
269+
const [m, n] = [grid.length, grid[0].length];
270+
const DIRS = [-1, 0, 1, 0, -1];
271+
const q = new MinPriorityQueue({ priority: ([x]) => x });
272+
const dist: number[][] = Array.from({ length: m }, () =>
273+
new Array(n).fill(Number.POSITIVE_INFINITY),
274+
);
275+
dist[0][0] = 0;
276+
q.enqueue([0, 0, 0]);
277+
278+
while (true) {
279+
const [t, i, j] = q.dequeue().element;
280+
if (i === m - 1 && j === n - 1) return t;
281+
282+
for (let k = 0; k < 4; k++) {
283+
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
284+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
285+
286+
let nt = t + 1;
287+
if (nt < grid[x][y]) {
288+
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
289+
}
290+
if (nt < dist[x][y]) {
291+
dist[x][y] = nt;
292+
q.enqueue([nt, x, y]);
293+
}
294+
}
295+
}
296+
}
297+
```
298+
299+
#### JavaScript
300+
301+
```js
302+
function minimumTime(grid) {
303+
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;
304+
305+
const [m, n] = [grid.length, grid[0].length];
306+
const DIRS = [-1, 0, 1, 0, -1];
307+
const q = new MinPriorityQueue({ priority: ([x]) => x });
308+
const dist = Array.from({ length: m }, () => new Array(n).fill(Number.POSITIVE_INFINITY));
309+
dist[0][0] = 0;
310+
q.enqueue([0, 0, 0]);
311+
312+
while (true) {
313+
const [t, i, j] = q.dequeue().element;
314+
if (i === m - 1 && j === n - 1) return t;
315+
316+
for (let k = 0; k < 4; k++) {
317+
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
318+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
319+
320+
let nt = t + 1;
321+
if (nt < grid[x][y]) {
322+
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
323+
}
324+
if (nt < dist[x][y]) {
325+
dist[x][y] = nt;
326+
q.enqueue([nt, x, y]);
327+
}
328+
}
329+
}
330+
}
331+
```
332+
263333
<!-- tabs:end -->
264334

265335
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function minimumTime(grid) {
2+
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;
3+
4+
const [m, n] = [grid.length, grid[0].length];
5+
const DIRS = [-1, 0, 1, 0, -1];
6+
const q = new MinPriorityQueue({ priority: ([x]) => x });
7+
const dist = Array.from({ length: m }, () => new Array(n).fill(Number.POSITIVE_INFINITY));
8+
dist[0][0] = 0;
9+
q.enqueue([0, 0, 0]);
10+
11+
while (true) {
12+
const [t, i, j] = q.dequeue().element;
13+
if (i === m - 1 && j === n - 1) return t;
14+
15+
for (let k = 0; k < 4; k++) {
16+
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
17+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
18+
19+
let nt = t + 1;
20+
if (nt < grid[x][y]) {
21+
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
22+
}
23+
if (nt < dist[x][y]) {
24+
dist[x][y] = nt;
25+
q.enqueue([nt, x, y]);
26+
}
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function minimumTime(grid: number[][]): number {
2+
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;
3+
4+
const [m, n] = [grid.length, grid[0].length];
5+
const DIRS = [-1, 0, 1, 0, -1];
6+
const q = new MinPriorityQueue({ priority: ([x]) => x });
7+
const dist: number[][] = Array.from({ length: m }, () =>
8+
new Array(n).fill(Number.POSITIVE_INFINITY),
9+
);
10+
dist[0][0] = 0;
11+
q.enqueue([0, 0, 0]);
12+
13+
while (true) {
14+
const [t, i, j] = q.dequeue().element;
15+
if (i === m - 1 && j === n - 1) return t;
16+
17+
for (let k = 0; k < 4; k++) {
18+
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
19+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
20+
21+
let nt = t + 1;
22+
if (nt < grid[x][y]) {
23+
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
24+
}
25+
if (nt < dist[x][y]) {
26+
dist[x][y] = nt;
27+
q.enqueue([nt, x, y]);
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)