We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c2da14a commit b552910Copy full SHA for b552910
Swift/62. Unique Paths.swift
@@ -0,0 +1,20 @@
1
+// 62. Unique Paths
2
+// 16 ms, 39.53%
3
+func uniquePaths(_ m: Int, _ n: Int) -> Int {
4
+ pathMatrix = Array(repeating: Array(repeating: 0, count: n+1), count: m+1)
5
+ return pathMatrix(m, n)
6
+}
7
+
8
+private var pathMatrix: [[Int]]!
9
+private func pathMatrix(_ m: Int, _ n: Int) -> Int {
10
+ if m <= 0 || n <= 0 { return 0 }
11
+ if m < 2 || n < 2 { pathMatrix[m][n] = 1; return 1 }
12
13
+ if pathMatrix[m-1][n] == 0 {
14
+ pathMatrix[m-1][n] = pathMatrix(m-1, n)
15
+ }
16
+ if pathMatrix[m][n-1] == 0 {
17
+ pathMatrix[m][n-1] = pathMatrix(m, n-1)
18
19
+ return pathMatrix[m-1][n] + pathMatrix[m][n-1]
20
0 commit comments