From d6140ef557fd93f2abb87d3e16a6c4449e292a17 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 31 May 2024 01:24:20 +0300 Subject: [PATCH 1/5] feat: add TS solution to lc problem No.1293 --- .../README.md | 49 +++++++++++++++++++ .../README_EN.md | 49 +++++++++++++++++++ .../Solution.ts | 44 +++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.ts diff --git a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md index a235d42f1bd59..5d5956facd28f 100644 --- a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md +++ b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md @@ -230,6 +230,55 @@ func shortestPath(grid [][]int, k int) int { } ``` +#### TypeScript + +```ts +function shortestPath(grid: number[][], k: number): number { + const n = grid.length - 1; + const m = grid[0].length - 1; + const vis = new Set(); + const dirs = [0, 1, 0, -1, 0]; + let q: Point[] = [[0, 0, k]]; + let steps = 0; + + if (k >= n + m - 1) return n + m; + if (!n && !m) return 0; + + while (q.length) { + const nextQ: Point[] = []; + steps++; + + while (q.length) { + const [y, x, k] = q.pop()!; + const key = [y, x, k].toString(); + + if (vis.has(key)) continue; + vis.add(key); + + for (let i = 0; i < 4; i++) { + const [dy, dx] = [dirs[i], dirs[i + 1]]; + const nextPoint: Point = [y + dy, x + dx, k]; + const value = grid[nextPoint[0]]?.[nextPoint[1]]; + + if (nextPoint[0] === n && nextPoint[1] === m) return steps; + + if (value === 0) nextQ.push(nextPoint); + else if (value === 1 && nextPoint[2]) { + nextPoint[2]--; + nextQ.push(nextPoint); + } + } + } + + q = nextQ; + } + + return -1; +} + +type Point = [number, number, number]; +``` + diff --git a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README_EN.md b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README_EN.md index f26a9577d683d..7df18bcc40f0e 100644 --- a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README_EN.md +++ b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README_EN.md @@ -224,6 +224,55 @@ func shortestPath(grid [][]int, k int) int { } ``` +#### TypeScript + +```ts +function shortestPath(grid: number[][], k: number): number { + const n = grid.length - 1; + const m = grid[0].length - 1; + const vis = new Set(); + const dirs = [0, 1, 0, -1, 0]; + let q: Point[] = [[0, 0, k]]; + let steps = 0; + + if (k >= n + m - 1) return n + m; + if (!n && !m) return 0; + + while (q.length) { + const nextQ: Point[] = []; + steps++; + + while (q.length) { + const [y, x, k] = q.pop()!; + const key = [y, x, k].toString(); + + if (vis.has(key)) continue; + vis.add(key); + + for (let i = 0; i < 4; i++) { + const [dy, dx] = [dirs[i], dirs[i + 1]]; + const nextPoint: Point = [y + dy, x + dx, k]; + const value = grid[nextPoint[0]]?.[nextPoint[1]]; + + if (nextPoint[0] === n && nextPoint[1] === m) return steps; + + if (value === 0) nextQ.push(nextPoint); + else if (value === 1 && nextPoint[2]) { + nextPoint[2]--; + nextQ.push(nextPoint); + } + } + } + + q = nextQ; + } + + return -1; +} + +type Point = [number, number, number]; +``` + diff --git a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.ts b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.ts new file mode 100644 index 0000000000000..68bbcf8674ece --- /dev/null +++ b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.ts @@ -0,0 +1,44 @@ +function shortestPath(grid: number[][], k: number): number { + const n = grid.length - 1; + const m = grid[0].length - 1; + const vis = new Set(); + const dirs = [0, 1, 0, -1, 0]; + let q: Point[] = [[0, 0, k]]; + let steps = 0; + + if (k >= n + m - 1) return n + m; + if (!n && !m) return 0; + + while (q.length) { + const nextQ: Point[] = []; + steps++; + + while (q.length) { + const [y, x, k] = q.pop()!; + const key = [y, x, k].toString(); + + if (vis.has(key)) continue; + vis.add(key); + + for (let i = 0; i < 4; i++) { + const [dy, dx] = [dirs[i], dirs[i + 1]]; + const nextPoint: Point = [y + dy, x + dx, k]; + const value = grid[nextPoint[0]]?.[nextPoint[1]]; + + if (nextPoint[0] === n && nextPoint[1] === m) return steps; + + if (value === 0) nextQ.push(nextPoint); + else if (value === 1 && nextPoint[2]) { + nextPoint[2]--; + nextQ.push(nextPoint); + } + } + } + + q = nextQ; + } + + return -1; +} + +type Point = [number, number, number]; From a51027ab5dc5ecbdd57b72d3197a692db7a88f9a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 31 May 2024 08:34:07 +0800 Subject: [PATCH 2/5] Update README.md --- .../README.md | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md index 5d5956facd28f..757ad7080cf87 100644 --- a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md +++ b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md @@ -234,45 +234,38 @@ func shortestPath(grid [][]int, k int) int { ```ts function shortestPath(grid: number[][], k: number): number { - const n = grid.length - 1; - const m = grid[0].length - 1; - const vis = new Set(); - const dirs = [0, 1, 0, -1, 0]; + const m = grid.length; + const n = grid[0].length; + if (k >= m + n - 3) { + return m + n - 2; + } let q: Point[] = [[0, 0, k]]; - let steps = 0; - - if (k >= n + m - 1) return n + m; - if (!n && !m) return 0; - + const vis = Array.from({ length: m }, () => + Array.from({ length: n }, () => Array.from({ length: k + 1 }, () => false)), + ); + const dirs = [0, 1, 0, -1, 0]; + let ans = 0; while (q.length) { const nextQ: Point[] = []; - steps++; - - while (q.length) { - const [y, x, k] = q.pop()!; - const key = [y, x, k].toString(); - - if (vis.has(key)) continue; - vis.add(key); - - for (let i = 0; i < 4; i++) { - const [dy, dx] = [dirs[i], dirs[i + 1]]; - const nextPoint: Point = [y + dy, x + dx, k]; - const value = grid[nextPoint[0]]?.[nextPoint[1]]; - - if (nextPoint[0] === n && nextPoint[1] === m) return steps; - - if (value === 0) nextQ.push(nextPoint); - else if (value === 1 && nextPoint[2]) { - nextPoint[2]--; - nextQ.push(nextPoint); + ++ans; + for (const [i, j, k] of q) { + for (let d = 0; d < 4; ++d) { + const [x, y] = [i + dirs[d], j + dirs[d + 1]]; + if (x === m - 1 && y === n - 1) { + return ans; + } + const v = grid[x]?.[y]; + if (v === 0 && !vis[x][y][k]) { + vis[x][y][k] = true; + nextQ.push([x, y, k]); + } else if (v === 1 && k > 0 && !vis[x][y][k - 1]) { + vis[x][y][k - 1] = true; + nextQ.push([x, y, k - 1]); } } } - q = nextQ; } - return -1; } From 29a067124b27e6505c25938cecb6d1a87e1516da Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 31 May 2024 08:35:07 +0800 Subject: [PATCH 3/5] Update README_EN.md --- .../README_EN.md | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README_EN.md b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README_EN.md index 7df18bcc40f0e..6a5529bc24330 100644 --- a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README_EN.md +++ b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README_EN.md @@ -228,45 +228,42 @@ func shortestPath(grid [][]int, k int) int { ```ts function shortestPath(grid: number[][], k: number): number { - const n = grid.length - 1; - const m = grid[0].length - 1; - const vis = new Set(); - const dirs = [0, 1, 0, -1, 0]; - let q: Point[] = [[0, 0, k]]; - let steps = 0; + const m = grid.length; + const n = grid[0].length; + if (k >= m + n - 3) { + return m + n - 2; + } - if (k >= n + m - 1) return n + m; - if (!n && !m) return 0; + let q: Point[] = [[0, 0, k]]; + const vis = Array.from({ length: m }, () => + Array.from({ length: n }, () => Array.from({ length: k + 1 }, () => false)), + ); + vis[0][0][k] = true; + const dirs = [0, 1, 0, -1, 0]; + let ans = 0; while (q.length) { const nextQ: Point[] = []; - steps++; - - while (q.length) { - const [y, x, k] = q.pop()!; - const key = [y, x, k].toString(); + ++ans; - if (vis.has(key)) continue; - vis.add(key); - - for (let i = 0; i < 4; i++) { - const [dy, dx] = [dirs[i], dirs[i + 1]]; - const nextPoint: Point = [y + dy, x + dx, k]; - const value = grid[nextPoint[0]]?.[nextPoint[1]]; - - if (nextPoint[0] === n && nextPoint[1] === m) return steps; - - if (value === 0) nextQ.push(nextPoint); - else if (value === 1 && nextPoint[2]) { - nextPoint[2]--; - nextQ.push(nextPoint); + for (const [i, j, k] of q) { + for (let d = 0; d < 4; ++d) { + const [x, y] = [i + dirs[d], j + dirs[d + 1]]; + if (x === m - 1 && y === n - 1) { + return ans; + } + const v = grid[x]?.[y]; + if (v === 0 && !vis[x][y][k]) { + nextQ.push([x, y, k]); + vis[x][y][k] = true; + } else if (v === 1 && k > 0 && !vis[x][y][k - 1]) { + nextQ.push([x, y, k - 1]); + vis[x][y][k - 1] = true; } } } - q = nextQ; } - return -1; } From ca1b6e05f4cf914e37a1430a873f7621e189db91 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 31 May 2024 08:35:23 +0800 Subject: [PATCH 4/5] Update README.md --- .../README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md index 757ad7080cf87..c63ce884f6f6f 100644 --- a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md +++ b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/README.md @@ -239,15 +239,19 @@ function shortestPath(grid: number[][], k: number): number { if (k >= m + n - 3) { return m + n - 2; } + let q: Point[] = [[0, 0, k]]; const vis = Array.from({ length: m }, () => Array.from({ length: n }, () => Array.from({ length: k + 1 }, () => false)), ); + vis[0][0][k] = true; const dirs = [0, 1, 0, -1, 0]; let ans = 0; + while (q.length) { const nextQ: Point[] = []; ++ans; + for (const [i, j, k] of q) { for (let d = 0; d < 4; ++d) { const [x, y] = [i + dirs[d], j + dirs[d + 1]]; @@ -256,11 +260,11 @@ function shortestPath(grid: number[][], k: number): number { } const v = grid[x]?.[y]; if (v === 0 && !vis[x][y][k]) { - vis[x][y][k] = true; nextQ.push([x, y, k]); + vis[x][y][k] = true; } else if (v === 1 && k > 0 && !vis[x][y][k - 1]) { - vis[x][y][k - 1] = true; nextQ.push([x, y, k - 1]); + vis[x][y][k - 1] = true; } } } From 753c892e44abdb0ea20fdb04ef86ec9985c19325 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 31 May 2024 08:36:04 +0800 Subject: [PATCH 5/5] Update Solution.ts --- .../Solution.ts | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.ts b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.ts index 68bbcf8674ece..e6dfdac769a89 100644 --- a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.ts +++ b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.ts @@ -1,43 +1,40 @@ function shortestPath(grid: number[][], k: number): number { - const n = grid.length - 1; - const m = grid[0].length - 1; - const vis = new Set(); - const dirs = [0, 1, 0, -1, 0]; - let q: Point[] = [[0, 0, k]]; - let steps = 0; + const m = grid.length; + const n = grid[0].length; + if (k >= m + n - 3) { + return m + n - 2; + } - if (k >= n + m - 1) return n + m; - if (!n && !m) return 0; + let q: Point[] = [[0, 0, k]]; + const vis = Array.from({ length: m }, () => + Array.from({ length: n }, () => Array.from({ length: k + 1 }, () => false)), + ); + vis[0][0][k] = true; + const dirs = [0, 1, 0, -1, 0]; + let ans = 0; while (q.length) { const nextQ: Point[] = []; - steps++; - - while (q.length) { - const [y, x, k] = q.pop()!; - const key = [y, x, k].toString(); + ++ans; - if (vis.has(key)) continue; - vis.add(key); - - for (let i = 0; i < 4; i++) { - const [dy, dx] = [dirs[i], dirs[i + 1]]; - const nextPoint: Point = [y + dy, x + dx, k]; - const value = grid[nextPoint[0]]?.[nextPoint[1]]; - - if (nextPoint[0] === n && nextPoint[1] === m) return steps; - - if (value === 0) nextQ.push(nextPoint); - else if (value === 1 && nextPoint[2]) { - nextPoint[2]--; - nextQ.push(nextPoint); + for (const [i, j, k] of q) { + for (let d = 0; d < 4; ++d) { + const [x, y] = [i + dirs[d], j + dirs[d + 1]]; + if (x === m - 1 && y === n - 1) { + return ans; + } + const v = grid[x]?.[y]; + if (v === 0 && !vis[x][y][k]) { + nextQ.push([x, y, k]); + vis[x][y][k] = true; + } else if (v === 1 && k > 0 && !vis[x][y][k - 1]) { + nextQ.push([x, y, k - 1]); + vis[x][y][k - 1] = true; } } } - q = nextQ; } - return -1; }