diff --git a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README.md b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README.md index ab8618f39a512..bb607910dd2ce 100644 --- a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README.md +++ b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README.md @@ -271,31 +271,31 @@ func (h *hp) Pop() (v any) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; re ```ts function minTimeToReach(moveTime: number[][]): number { - const [n, m] = [moveTime.length, moveTime[0].length]; - const dist: number[][] = Array.from({ length: n }, () => Array(m).fill(Infinity)); + const n = moveTime.length; + const m = moveTime[0].length; + const dist = Array.from({ length: n }, () => Array(m).fill(Infinity)); dist[0][0] = 0; - const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] }); + type Node = [number, number, number]; + const pq = new PriorityQueue((a, b) => a[0] - b[0]); pq.enqueue([0, 0, 0]); const dirs = [-1, 0, 1, 0, -1]; - while (1) { + while (!pq.isEmpty()) { const [d, i, j] = pq.dequeue(); - if (i === n - 1 && j === m - 1) { - return d; - } - if (d > dist[i][j]) { - continue; - } + if (d > dist[i][j]) continue; + if (i === n - 1 && j === m - 1) return d; for (let k = 0; k < 4; ++k) { - const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + const x = i + dirs[k]; + const y = j + dirs[k + 1]; if (x >= 0 && x < n && y >= 0 && y < m) { - const t = Math.max(moveTime[x][y], dist[i][j]) + 1; - if (dist[x][y] > t) { + const t = Math.max(moveTime[x][y], d) + 1; + if (t < dist[x][y]) { dist[x][y] = t; pq.enqueue([t, x, y]); } } } } + return -1; } ``` diff --git a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README_EN.md b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README_EN.md index eb0c654f6e62d..4af0e8b859354 100644 --- a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README_EN.md +++ b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/README_EN.md @@ -268,31 +268,31 @@ func (h *hp) Pop() (v any) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; re ```ts function minTimeToReach(moveTime: number[][]): number { - const [n, m] = [moveTime.length, moveTime[0].length]; - const dist: number[][] = Array.from({ length: n }, () => Array(m).fill(Infinity)); + const n = moveTime.length; + const m = moveTime[0].length; + const dist = Array.from({ length: n }, () => Array(m).fill(Infinity)); dist[0][0] = 0; - const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] }); + type Node = [number, number, number]; + const pq = new PriorityQueue((a, b) => a[0] - b[0]); pq.enqueue([0, 0, 0]); const dirs = [-1, 0, 1, 0, -1]; - while (1) { + while (!pq.isEmpty()) { const [d, i, j] = pq.dequeue(); - if (i === n - 1 && j === m - 1) { - return d; - } - if (d > dist[i][j]) { - continue; - } + if (d > dist[i][j]) continue; + if (i === n - 1 && j === m - 1) return d; for (let k = 0; k < 4; ++k) { - const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + const x = i + dirs[k]; + const y = j + dirs[k + 1]; if (x >= 0 && x < n && y >= 0 && y < m) { - const t = Math.max(moveTime[x][y], dist[i][j]) + 1; - if (dist[x][y] > t) { + const t = Math.max(moveTime[x][y], d) + 1; + if (t < dist[x][y]) { dist[x][y] = t; pq.enqueue([t, x, y]); } } } } + return -1; } ``` diff --git a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/Solution.ts b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/Solution.ts index c8722cbc032e7..5a020ae17f3c4 100644 --- a/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/Solution.ts +++ b/solution/3300-3399/3341.Find Minimum Time to Reach Last Room I/Solution.ts @@ -1,27 +1,27 @@ function minTimeToReach(moveTime: number[][]): number { - const [n, m] = [moveTime.length, moveTime[0].length]; - const dist: number[][] = Array.from({ length: n }, () => Array(m).fill(Infinity)); + const n = moveTime.length; + const m = moveTime[0].length; + const dist = Array.from({ length: n }, () => Array(m).fill(Infinity)); dist[0][0] = 0; - const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] }); + type Node = [number, number, number]; + const pq = new PriorityQueue((a, b) => a[0] - b[0]); pq.enqueue([0, 0, 0]); const dirs = [-1, 0, 1, 0, -1]; - while (1) { + while (!pq.isEmpty()) { const [d, i, j] = pq.dequeue(); - if (i === n - 1 && j === m - 1) { - return d; - } - if (d > dist[i][j]) { - continue; - } + if (d > dist[i][j]) continue; + if (i === n - 1 && j === m - 1) return d; for (let k = 0; k < 4; ++k) { - const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + const x = i + dirs[k]; + const y = j + dirs[k + 1]; if (x >= 0 && x < n && y >= 0 && y < m) { - const t = Math.max(moveTime[x][y], dist[i][j]) + 1; - if (dist[x][y] > t) { + const t = Math.max(moveTime[x][y], d) + 1; + if (t < dist[x][y]) { dist[x][y] = t; pq.enqueue([t, x, y]); } } } } + return -1; } diff --git a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README.md b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README.md index ee5bc219b1c5f..d113884f932fe 100644 --- a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README.md +++ b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README.md @@ -272,31 +272,31 @@ func (h *hp) Pop() (v any) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; re ```ts function minTimeToReach(moveTime: number[][]): number { - const [n, m] = [moveTime.length, moveTime[0].length]; - const dist: number[][] = Array.from({ length: n }, () => Array(m).fill(Infinity)); + const n = moveTime.length; + const m = moveTime[0].length; + const dist = Array.from({ length: n }, () => Array(m).fill(Infinity)); dist[0][0] = 0; - const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] }); + type Node = [number, number, number]; + const pq = new PriorityQueue((a, b) => a[0] - b[0]); pq.enqueue([0, 0, 0]); const dirs = [-1, 0, 1, 0, -1]; - while (1) { + while (!pq.isEmpty()) { const [d, i, j] = pq.dequeue(); - if (i === n - 1 && j === m - 1) { - return d; - } - if (d > dist[i][j]) { - continue; - } - for (let k = 0; k < 4; ++k) { - const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + if (d > dist[i][j]) continue; + if (i === n - 1 && j === m - 1) return d; + for (let k = 0; k < 4; k++) { + const x = i + dirs[k]; + const y = j + dirs[k + 1]; if (x >= 0 && x < n && y >= 0 && y < m) { - const t = Math.max(moveTime[x][y], dist[i][j]) + ((i + j) % 2) + 1; - if (dist[x][y] > t) { + const t = Math.max(moveTime[x][y], d) + ((i + j) % 2) + 1; + if (t < dist[x][y]) { dist[x][y] = t; pq.enqueue([t, x, y]); } } } } + return -1; } ``` diff --git a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README_EN.md b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README_EN.md index e9dd0768112ee..fd4dc3f1e26cf 100644 --- a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README_EN.md +++ b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/README_EN.md @@ -269,31 +269,31 @@ func (h *hp) Pop() (v any) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; re ```ts function minTimeToReach(moveTime: number[][]): number { - const [n, m] = [moveTime.length, moveTime[0].length]; - const dist: number[][] = Array.from({ length: n }, () => Array(m).fill(Infinity)); + const n = moveTime.length; + const m = moveTime[0].length; + const dist = Array.from({ length: n }, () => Array(m).fill(Infinity)); dist[0][0] = 0; - const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] }); + type Node = [number, number, number]; + const pq = new PriorityQueue((a, b) => a[0] - b[0]); pq.enqueue([0, 0, 0]); const dirs = [-1, 0, 1, 0, -1]; - while (1) { + while (!pq.isEmpty()) { const [d, i, j] = pq.dequeue(); - if (i === n - 1 && j === m - 1) { - return d; - } - if (d > dist[i][j]) { - continue; - } - for (let k = 0; k < 4; ++k) { - const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + if (d > dist[i][j]) continue; + if (i === n - 1 && j === m - 1) return d; + for (let k = 0; k < 4; k++) { + const x = i + dirs[k]; + const y = j + dirs[k + 1]; if (x >= 0 && x < n && y >= 0 && y < m) { - const t = Math.max(moveTime[x][y], dist[i][j]) + ((i + j) % 2) + 1; - if (dist[x][y] > t) { + const t = Math.max(moveTime[x][y], d) + ((i + j) % 2) + 1; + if (t < dist[x][y]) { dist[x][y] = t; pq.enqueue([t, x, y]); } } } } + return -1; } ``` diff --git a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/Solution.ts b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/Solution.ts index 6d0284e4bd449..9d9af3af47d99 100644 --- a/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/Solution.ts +++ b/solution/3300-3399/3342.Find Minimum Time to Reach Last Room II/Solution.ts @@ -1,27 +1,27 @@ function minTimeToReach(moveTime: number[][]): number { - const [n, m] = [moveTime.length, moveTime[0].length]; - const dist: number[][] = Array.from({ length: n }, () => Array(m).fill(Infinity)); + const n = moveTime.length; + const m = moveTime[0].length; + const dist = Array.from({ length: n }, () => Array(m).fill(Infinity)); dist[0][0] = 0; - const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] }); + type Node = [number, number, number]; + const pq = new PriorityQueue((a, b) => a[0] - b[0]); pq.enqueue([0, 0, 0]); const dirs = [-1, 0, 1, 0, -1]; - while (1) { + while (!pq.isEmpty()) { const [d, i, j] = pq.dequeue(); - if (i === n - 1 && j === m - 1) { - return d; - } - if (d > dist[i][j]) { - continue; - } - for (let k = 0; k < 4; ++k) { - const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + if (d > dist[i][j]) continue; + if (i === n - 1 && j === m - 1) return d; + for (let k = 0; k < 4; k++) { + const x = i + dirs[k]; + const y = j + dirs[k + 1]; if (x >= 0 && x < n && y >= 0 && y < m) { - const t = Math.max(moveTime[x][y], dist[i][j]) + ((i + j) % 2) + 1; - if (dist[x][y] > t) { + const t = Math.max(moveTime[x][y], d) + ((i + j) % 2) + 1; + if (t < dist[x][y]) { dist[x][y] = t; pq.enqueue([t, x, y]); } } } } + return -1; }