Skip to content

Commit dc5a3e7

Browse files
committed
Create 0062-unique-paths.swift
1 parent 8d212e4 commit dc5a3e7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

swift/0062-unique-paths.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/unique-paths/
3+
*/
4+
5+
class Solution {
6+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
7+
var prevRow = [Int](repeating: 0, count: n)
8+
for r in stride(from: m - 1, to: -1, by: -1) {
9+
var curRow = [Int](repeating: 0, count: n)
10+
curRow[n - 1] = 1
11+
for c in stride(from: n - 2, to: -1, by: -1) {
12+
curRow[c] = curRow[c + 1] + prevRow[c]
13+
}
14+
prevRow = curRow
15+
}
16+
return prevRow[0]
17+
}
18+
}

0 commit comments

Comments
 (0)