forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcoFriendlyAppleSu.kt
More file actions
29 lines (25 loc) ยท 910 Bytes
/
EcoFriendlyAppleSu.kt
File metadata and controls
29 lines (25 loc) ยท 910 Bytes
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
package leetcode_study
/*
* ์ฃผ์ด์ง m,n size board ์์์ ์ข์ธก ์๋ถํฐ ์ฐ์ธก ์๋๊น์ง ๋์ฐฉํ๋ Unique path์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๋ฐฉ๋ฒ (m = row size, n = col size)
* ์์ง์ผ ์ ์๋ ๋ฐฉํฅ์ด ์๋์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ ํด์ ์๋ ์ํฉ์์ ๋ค์ ์นธ์ผ๋ก ๊ฐ ์ ์๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ์
* board[i][j] = board[i][j-1] + board[i-1][j]
* ์๊ฐ ๋ณต์ก๋: O(mn)
* -> board๋ฅผ ์ํํ๋ฉฐ unique path๋ฅผ ๊ตฌํ๋ ๊ณผ์
* ๊ณต๊ฐ ๋ณต์ก๋: O(mn)
* -> m, n์ ์ฌ์ฉํด board๋ฅผ ๊ตฌ์ฑ
* */
fun uniquePaths(m: Int, n: Int): Int {
val board = Array(m) { IntArray(n) { 0 } }
for (i in IntRange(0, m-1)) {
board[i][0] = 1
}
for (i in IntRange(0, n-1)) {
board[0][i] = 1
}
for (i in 1 until m) {
for (j in 1 until n) {
board[i][j] = board[i][j-1] + board[i-1][j]
}
}
return board[m-1][n-1]
}