Skip to content

Commit f001f5b

Browse files
author
แ„‹แ…ตแ„‹แ…งแ†ซแ„‰แ…ฎ
committed
unique paths
1 parent 8deee9f commit f001f5b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package leetcode_study
2+
3+
/*
4+
* ์ฃผ์–ด์ง„ m,n size board ์œ„์—์„œ ์ขŒ์ธก ์œ„๋ถ€ํ„ฐ ์šฐ์ธก ์•„๋ž˜๊นŒ์ง€ ๋„์ฐฉํ•˜๋Š” Unique path์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ• (m = row size, n = col size)
5+
* ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์ด ์•„๋ž˜์™€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •ํ•ด์ € ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ๋‹ค์Œ ์นธ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Œ
6+
* board[i][j] = board[i][j-1] + board[i-1][j]
7+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(mn)
8+
* -> board๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ unique path๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •
9+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(mn)
10+
* -> m, n์„ ์‚ฌ์šฉํ•ด board๋ฅผ ๊ตฌ์„ฑ
11+
* */
12+
fun uniquePaths(m: Int, n: Int): Int {
13+
val board = Array(m) { IntArray(n) { 0 } }
14+
15+
for (i in IntRange(0, m-1)) {
16+
board[i][0] = 1
17+
}
18+
for (i in IntRange(0, n-1)) {
19+
board[0][i] = 1
20+
}
21+
22+
for (i in 1 until m) {
23+
for (j in 1 until n) {
24+
board[i][j] = board[i][j-1] + board[i-1][j]
25+
}
26+
}
27+
28+
return board[m-1][n-1]
29+
}

0 commit comments

Comments
ย (0)