Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57dff98

Browse files
authored
Merge pull request neetcode-gh#3349 from drxlx/0063-unique-paths-ii
Create 0063-unique-paths-ii.swift
2 parents 5d2d284 + c685dc0 commit 57dff98

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

swift/0063-unique-paths-ii.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/unique-paths-ii/
3+
*/
4+
5+
class Solution {
6+
func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int {
7+
let rows = obstacleGrid.count
8+
let cols = obstacleGrid[0].count
9+
var dp = [Int](repeating: 0, count: cols)
10+
dp[cols - 1] = 1
11+
12+
for r in stride(from: rows - 1, to: -1, by: -1) {
13+
for c in stride(from: cols - 1, to: -1, by: -1) {
14+
if obstacleGrid[r][c] == 1 {
15+
dp[c] = 0
16+
} else if c + 1 < cols {
17+
dp[c] = dp[c] + dp[c + 1]
18+
}
19+
}
20+
}
21+
return dp[0]
22+
}
23+
}

0 commit comments

Comments
 (0)