Last active
April 28, 2021 12:38
-
-
Save vrat28/64e94d646c9b9913e4adc7e4d1c5f02c to your computer and use it in GitHub Desktop.
Unique Paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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