Skip to content

Commit 4151885

Browse files
committed
Add a solution to Shortest Path in a Grid with Obstacles Elimination
1 parent 34b53ab commit 4151885

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/
3+
* Primary idea: BFS to go over all possible word paths until the end. Choose the path only the remaining elimination number is greater.
4+
*
5+
* Time Complexity: O(nmk), Space Complexity: O(nm)
6+
*/
7+
8+
class ShortestPathGridObstaclesElimination {
9+
func shortestPath(_ grid: [[Int]], _ k: Int) -> Int {
10+
let m = grid.count, n = grid[0].count
11+
var remainings = Array(repeating: Array(repeating: -1, count: n), count: m)
12+
13+
remainings[0][0] = k
14+
var queue = [Point(i: 0, j: 0, remain: k)]
15+
var count = 0
16+
17+
if k > m + n - 2 {
18+
return m + n - 2;
19+
}
20+
21+
while !queue.isEmpty {
22+
23+
let currentPoints = queue
24+
queue.removeAll()
25+
26+
for point in currentPoints {
27+
if point.i == m - 1 && point.j == n - 1 {
28+
return count
29+
}
30+
31+
for dir in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
32+
let (x, y) = (point.i + dir.0, point.j + dir.1)
33+
34+
guard x >= 0 && x < m && y >= 0 && y < n else {
35+
continue
36+
}
37+
38+
if grid[x][y] == 1 && point.remain <= 0 {
39+
continue
40+
}
41+
42+
let remain = grid[x][y] == 1 ? point.remain - 1 : point.remain
43+
// only choose the path if remainings are greater
44+
if remainings[x][y] >= remain {
45+
continue
46+
}
47+
48+
remainings[x][y] = remain
49+
queue.append(Point(i: x, j: y, remain: remain))
50+
}
51+
}
52+
count += 1
53+
}
54+
55+
return -1
56+
}
57+
58+
struct Point {
59+
var i: Int
60+
var j: Int
61+
var remain: Int
62+
}
63+
}

0 commit comments

Comments
 (0)