Skip to content

Commit d95b42d

Browse files
author
easyone
committed
Feat: Add solution of climbing-stairs
1 parent cfe82dc commit d95b42d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ํ’€์ด
2+
// 1 ์ผ ๋•Œ, ๊ฐ€๋Šฅํ•œ step์€ 1 -> 1๊ฐ€์ง€
3+
// 2 ์ผ ๋•Œ, ๊ฐ€๋Šฅํ•œ step์€ 1 1, 2 -> 2๊ฐ€์ง€
4+
// 3 ์ผ ๋•Œ, ๊ฐ€๋Šฅํ•œ step์€ 1 1 1, 1 2, 2 1 -> 3๊ฐ€์ง€
5+
// n ์ผ ๋•Œ, ๊ฐ€๋Šฅํ•œ stop์€ n-1์˜ ๊ฐ€์ง“์ˆ˜์— 1์ด ๋ถ™๊ณ , n-2์˜ ๊ฐ€์ง“์ˆ˜์— 2๊ฐ€ ๋ถ™๋Š”๋‹ค.
6+
7+
// TC
8+
// O(n)
9+
10+
// SC
11+
// intํƒ€์ž… ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•ด์„œ O(1)
12+
13+
func climbStairs(n int) int {
14+
if n <= 2 {
15+
return n
16+
}
17+
prev := 1 // climb1
18+
curr := 2 // climb2
19+
for i := 3; i <= n; i++ {
20+
prev, curr = curr, (curr + prev)
21+
}
22+
return curr
23+
}

0 commit comments

Comments
ย (0)