Skip to content

Commit c33a6c8

Browse files
committed
climbing-stairs
1 parent 6e7c9c1 commit c33a6c8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
3+
* - n = 5 ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์ƒ๊ฐํ•ด๋ณด๊ธฐ
4+
* - 3์ธต ๊ณ„๋‹จ -> 2์ธต, 1์ธต ๊ณ„์‚ฐ (์ด ๊ฒฝ์šฐ๋Š” ์ด๋ฏธ ๋ฉ”๋ชจ๋˜์–ด ์žˆ์–ด์„œ ๋ณ„๋„๋กœ ๊ณ„์‚ฐํ•˜์ง„ ์•Š์Œ)
5+
* - 4์ธต ๊ณ„๋‹จ -> 3์ธต, 2์ธต ๊ณ„์‚ฐ (2์ธต์€ ๋ฉ”๋ชจ์— ์žˆ๋Š” ๊ฒƒ ํ™œ์šฉ)
6+
* - 5์ธต ๊ณ„๋‹จ -> 4์ธต, 3์ธต ๊ณ„์‚ฐ (3์ธต์€ ๋ฉ”๋ชจ์— ์žˆ๋Š” ๊ฒƒ ํ™œ์šฉ)
7+
* - ...
8+
* - ๊ฐ ๋‹จ๊ณ„ ๋ณ„๋กœ (๋ฉ”๋ชจ๋ฅผ ํ™œ์šฉํ•ด) ์•„์ง ๊ณ„์‚ฐ๋˜์ง€ ์•Š์€ ๊ฒƒ์„ ํ•œ ๋ฒˆ์”ฉ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ O(n)
9+
* - ๊ทผ๋ฐ ๋งŒ์•ฝ ๋ฉ”๋ชจ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด? (์ค‘๋ณต ํ˜ธ์ถœ์ด ๋งŽ์ด ์ผ์–ด๋‚จ ... O(n^2))
10+
*
11+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
12+
*/
13+
class Solution {
14+
public int climbStairs(int n) {
15+
int[] memo = new int[n + 1];
16+
return climbStairs(n, memo);
17+
}
18+
19+
public int climbStairs(int n, int[] memo) {
20+
if (n == 1) return 1;
21+
if (n == 2) return 2;
22+
23+
if (memo[n] > 0) return memo[n];
24+
25+
memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo);
26+
return memo[n];
27+
}
28+
}

0 commit comments

Comments
ย (0)