forked from NilaakashSingh/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniquePaths.swift
29 lines (25 loc) · 849 Bytes
/
UniquePaths.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Question Link: https://leetcode.com/problems/unique-paths/
* Primary idea: 2D Dynamic Programming, use a 2D array as a cache to store calculated data
* Time Complexity: O(mn), Space Complexity: O(mn)
*
*/
class UniquePaths {
func uniquePaths(m: Int, _ n: Int) -> Int {
var pathNums = Array(count: m, repeatedValue: Array(count: n, repeatedValue: 0))
return _helper(&pathNums, m - 1, n - 1)
}
private func _helper(inout pathNums: [[Int]], _ m: Int, _ n: Int) -> Int {
if m < 0 || n < 0 {
return 0
}
if m == 0 || n == 0 {
return 1
}
if pathNums[m][n] != 0 {
return pathNums[m][n]
}
pathNums[m][n] = _helper(&pathNums, m - 1, n) + _helper(&pathNums, m, n - 1)
return pathNums[m][n]
}
}