Skip to content

Commit

Permalink
Add a solution to Shortest Path in a Grid with Obstacles Elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Sep 14, 2022
1 parent 34b53ab commit 4151885
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions BFS/ShortestPathGridObstaclesElimination.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Question Link: https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/
* Primary idea: BFS to go over all possible word paths until the end. Choose the path only the remaining elimination number is greater.
*
* Time Complexity: O(nmk), Space Complexity: O(nm)
*/

class ShortestPathGridObstaclesElimination {
func shortestPath(_ grid: [[Int]], _ k: Int) -> Int {
let m = grid.count, n = grid[0].count
var remainings = Array(repeating: Array(repeating: -1, count: n), count: m)

remainings[0][0] = k
var queue = [Point(i: 0, j: 0, remain: k)]
var count = 0

if k > m + n - 2 {
return m + n - 2;
}

while !queue.isEmpty {

let currentPoints = queue
queue.removeAll()

for point in currentPoints {
if point.i == m - 1 && point.j == n - 1 {
return count
}

for dir in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
let (x, y) = (point.i + dir.0, point.j + dir.1)

guard x >= 0 && x < m && y >= 0 && y < n else {
continue
}

if grid[x][y] == 1 && point.remain <= 0 {
continue
}

let remain = grid[x][y] == 1 ? point.remain - 1 : point.remain
// only choose the path if remainings are greater
if remainings[x][y] >= remain {
continue
}

remainings[x][y] = remain
queue.append(Point(i: x, j: y, remain: remain))
}
}
count += 1
}

return -1
}

struct Point {
var i: Int
var j: Int
var remain: Int
}
}

0 comments on commit 4151885

Please sign in to comment.