Skip to content

Instantly share code, notes, and snippets.

@vrat28
Last active April 28, 2021 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/64e94d646c9b9913e4adc7e4d1c5f02c to your computer and use it in GitHub Desktop.
Save vrat28/64e94d646c9b9913e4adc7e4d1c5f02c to your computer and use it in GitHub Desktop.
Unique Paths
class Solution {
func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int {
if obstacleGrid[0][0] == 1 { return 0}
let n = obstacleGrid.count, m = obstacleGrid[0].count
var dp = Array(repeating: Array(repeating:0, count: m), count:n)
dp[0][0] = 1
for i in 0..<n{
for j in 0..<m {
let element = obstacleGrid[i][j]
if element == 1 || (i == 0 && j == 0) {
continue
}
var total = 0
if i > 0 {
total += dp[i - 1][j]
}
if j > 0 {
total += dp[i][j - 1]
}
dp[i][j] = total
}
}
return dp[n - 1][m - 1]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment