Skip to content

Commit c0f19e9

Browse files
committed
Add solution #1293
1 parent badf852 commit c0f19e9

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,212 LeetCode solutions in JavaScript
1+
# 1,213 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -977,6 +977,7 @@
977977
1290|[Convert Binary Number in a Linked List to Integer](./solutions/1290-convert-binary-number-in-a-linked-list-to-integer.js)|Easy|
978978
1291|[Sequential Digits](./solutions/1291-sequential-digits.js)|Medium|
979979
1292|[Maximum Side Length of a Square with Sum Less than or Equal to Threshold](./solutions/1292-maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js)|Medium|
980+
1293|[Shortest Path in a Grid with Obstacles Elimination](./solutions/1293-shortest-path-in-a-grid-with-obstacles-elimination.js)|Hard|
980981
1295|[Find Numbers with Even Number of Digits](./solutions/1295-find-numbers-with-even-number-of-digits.js)|Easy|
981982
1296|[Divide Array in Sets of K Consecutive Numbers](./solutions/1296-divide-array-in-sets-of-k-consecutive-numbers.js)|Medium|
982983
1297|[Maximum Number of Occurrences of a Substring](./solutions/1297-maximum-number-of-occurrences-of-a-substring.js)|Medium|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1293. Shortest Path in a Grid with Obstacles Elimination
3+
* https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/
4+
* Difficulty: Hard
5+
*
6+
* You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle).
7+
* You can move up, down, left, or right from and to an empty cell in one step.
8+
*
9+
* Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right
10+
* corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to
11+
* find such walk return -1.
12+
*/
13+
14+
function shortestPath(grid, k) {
15+
const rows = grid.length;
16+
const cols = grid[0].length;
17+
if (rows === 1 && cols === 1) return 0;
18+
19+
let steps = 1;
20+
let queue = [[0, 0, k]];
21+
const visited = Array.from({ length: rows }, () => new Int16Array(cols).fill(-1));
22+
const directions = [0, -1, 0, 1, 0];
23+
24+
while (queue.length) {
25+
const nextLevel = [];
26+
for (const [row, col, obstaclesLeft] of queue) {
27+
for (let i = 0; i < 4; i++) {
28+
const newRow = row + directions[i];
29+
const newCol = col + directions[i + 1];
30+
31+
if (newRow < 0 || newRow === rows || newCol < 0
32+
|| newCol === cols || visited[newRow][newCol] >= obstaclesLeft) continue;
33+
34+
if (newRow === rows - 1 && newCol === cols - 1) return steps;
35+
36+
const remainingObstacles = grid[newRow][newCol] ? obstaclesLeft - 1 : obstaclesLeft;
37+
visited[newRow][newCol] = remainingObstacles;
38+
nextLevel.push([newRow, newCol, remainingObstacles]);
39+
}
40+
}
41+
steps++;
42+
queue = nextLevel;
43+
}
44+
45+
return -1;
46+
}

0 commit comments

Comments
 (0)