Skip to content

Commit 2cd028d

Browse files
committed
[Week7](gmlwls96) Unique Paths
1 parent 78c4ea8 commit 2cd028d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

unique-paths/gmlwls96.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
// 시간 : O(m*n) 공간 : O(m*n)
3+
// dp 알고리즘. pathMap[y][x]로 올수 있는 경로의 수는
4+
// 위쪽(pathMap[y - 1][x]) + 왼쪽( pathMap[y][x - 1]) 경로의 수의 합이다.
5+
fun uniquePaths(m: Int, n: Int): Int {
6+
val pathMap = Array(m) { y ->
7+
IntArray(n) { x ->
8+
if (y == 0 || x == 0) {
9+
1
10+
} else {
11+
0
12+
}
13+
}
14+
}
15+
for (y in 1 until m) {
16+
for (x in 1 until n) {
17+
pathMap[y][x] = pathMap[y - 1][x] + pathMap[y][x - 1]
18+
}
19+
}
20+
21+
return pathMap[m - 1][n - 1]
22+
}
23+
}

0 commit comments

Comments
 (0)