File tree Expand file tree Collapse file tree 1 file changed +23
-7
lines changed Expand file tree Collapse file tree 1 file changed +23
-7
lines changed Original file line number Diff line number Diff line change @@ -5,14 +5,30 @@ def climbStairs(self, n):
55 :rtype: int
66 f(n)์ด ๊ณ๋จ์ ์ฌ๋ผ๊ฐ ์ ์๋ ๋ฐฉ๋ฒ์ ์๋ผ๋ฉด,
77 f(n) = f(n-1) + f(n-2)
8+ - Time Complexity
9+ ์ฌ๊ท๋ก ํธ๋ ๊ฒฝ์ฐ ์ง์ ์๊ฐ์ด ์์๋์ง๋ง,
10+ ์๋์ ๊ฐ์ด ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ์ผ๋ก ํธ๋ ๊ฒฝ์ฐ
11+ O(n) ์์ ๋จ
12+ - Space Complexity
13+ ์ฌ๊ท๋ก ํธ๋ ๊ฒฝ์ฐ, ์ฌ๊ท ํธ์ถ ์คํ์ด O(n)๋งํผ์ ๊ณต๊ฐ์ ์ฌ์ฉํ ์ ์์
14+ ์๋์ ๊ฐ์ด ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ์ผ๋ก ํธ๋ ๊ฒฝ์ฐ,
15+ ๋ฆฌ์คํธ dp์ ๊ฐ ์ธ๋ฑ์ค ๋ณ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๋ฏ๋ก O(n) ์ฌ์ฉ ๋จ
816
917 """
10- # ๊ธฐ์ ์กฐ๊ฑด:
11- # n์ด 1์ธ ๊ฒฝ์ฐ 1 ๋ฐํ
18+
19+ # n์ด 1์ธ ๊ฒฝ์ฐ ๋ฐฉ๋ฒ์ ํ๋๋ฟ
1220 if n == 1 :
1321 return 1
14- # n์ด 2์ธ ๊ฒฝ์ฐ 2 ๋ฐํ
15- if n == 2 :
16- return 2
17- # ์ฌ๊ท ํธ์ถ
18- return self .climbStairs (n - 1 ) + self .climbStairs (n - 2 )
22+
23+ # ๊ธธ์ด๊ฐ n+1์ธ ๋ฆฌ์คํธ ์์ฑ
24+ # ์ธ๋ฑ์ค 0: ์์์ (0๋ฒ์งธ)
25+ # ์ธ๋ฑ์ค n: n๋ฒ์งธ ๊ณ๋จ
26+ dp = [0 for i in range (n + 1 )]
27+
28+ dp [1 ] = 1 # n = 1์ด๋ฉด 1์ ๋ฐํ
29+ dp [2 ] = 2 # n = 2์ด๋ฉด 2๋ฅผ ๋ฐํ
30+
31+ for i in range (3 , n + 1 ):
32+ dp [i ] = dp [i - 1 ] + dp [i - 2 ]
33+
34+ return dp [n ]
You canโt perform that action at this time.
0 commit comments