@@ -272,31 +272,31 @@ func (h *hp) Pop() (v any) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; re
272
272
273
273
``` ts
274
274
function minTimeToReach(moveTime : number [][]): number {
275
- const [n, m] = [moveTime .length , moveTime [0 ].length ];
276
- const dist: number [][] = Array .from ({ length: n }, () => Array (m ).fill (Infinity ));
275
+ const n = moveTime .length ;
276
+ const m = moveTime [0 ].length ;
277
+ const dist = Array .from ({ length: n }, () => Array (m ).fill (Infinity ));
277
278
dist [0 ][0 ] = 0 ;
278
- const pq = new PriorityQueue ({ compare : (a , b ) => a [0 ] - b [0 ] });
279
+ type Node = [number , number , number ];
280
+ const pq = new PriorityQueue <Node >((a , b ) => a [0 ] - b [0 ]);
279
281
pq .enqueue ([0 , 0 , 0 ]);
280
282
const dirs = [- 1 , 0 , 1 , 0 , - 1 ];
281
- while (1 ) {
283
+ while (! pq . isEmpty () ) {
282
284
const [d, i, j] = pq .dequeue ();
283
- if (i === n - 1 && j === m - 1 ) {
284
- return d ;
285
- }
286
- if (d > dist [i ][j ]) {
287
- continue ;
288
- }
289
- for (let k = 0 ; k < 4 ; ++ k ) {
290
- const [x, y] = [i + dirs [k ], j + dirs [k + 1 ]];
285
+ if (d > dist [i ][j ]) continue ;
286
+ if (i === n - 1 && j === m - 1 ) return d ;
287
+ for (let k = 0 ; k < 4 ; k ++ ) {
288
+ const x = i + dirs [k ];
289
+ const y = j + dirs [k + 1 ];
291
290
if (x >= 0 && x < n && y >= 0 && y < m ) {
292
- const t = Math .max (moveTime [x ][y ], dist [ i ][ j ] ) + ((i + j ) % 2 ) + 1 ;
293
- if (dist [x ][y ] > t ) {
291
+ const t = Math .max (moveTime [x ][y ], d ) + ((i + j ) % 2 ) + 1 ;
292
+ if (t < dist [x ][y ]) {
294
293
dist [x ][y ] = t ;
295
294
pq .enqueue ([t , x , y ]);
296
295
}
297
296
}
298
297
}
299
298
}
299
+ return - 1 ;
300
300
}
301
301
```
302
302
0 commit comments