Skip to content

Instantly share code, notes, and snippets.

@charlieInDen
Created September 16, 2020 10:57
Show Gist options
  • Save charlieInDen/314b1ca0ae1382ddd2e70ba8a8d69275 to your computer and use it in GitHub Desktop.
Save charlieInDen/314b1ca0ae1382ddd2e70ba8a8d69275 to your computer and use it in GitHub Desktop.
uniquePathsWithObstacles
class Solution {
func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int {
var grid = obstacleGrid
let row = grid.count
let col = grid[0].count
//Base case
if grid.isEmpty || grid[0][0] == 1 { return 0 }
grid[0][0] = 1
for i in 1..<row {
grid[i][0] = (grid[i][0] == 0 && grid[i-1][0] == 1) ? 1 : 0
}
for j in 1..<col {
grid[0][j] = (grid[0][j] == 0 && grid[0][j - 1] == 1) ? 1 : 0
}
for i in 1..<row {
for j in 1..<col {
if grid[i][j] == 0 {
grid[i][j] = grid[i-1][j] + grid[i][j-1]
} else {
grid[i][j] = 0
}
}
}
return grid[row - 1][col - 1]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment