File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ i๋ฒ์งธ ์นธ์ ๊ฐ๋ ๋ฐฉ๋ฒ์ (1) i-2๋ฒ์งธ ์นธ์์ 2์นธ์ ์ ํํ๊ฑฐ๋ (2) i-1๋ฒ์งธ ์นธ์์ 1์นธ์ ์ ํํ๋ 2๊ฐ์ง ๋ฐฉ๋ฒ ๋ฟ์
๋๋ค. (MECEํจ)
3
+ ๋ฐ๋ผ์, (i๋ฒ์งธ ์นธ์ ๊ฐ๋ ๊ฒฝ์ฐ์ ์) = (i-2๋ฒ์งธ ์นธ์ ๊ฐ๋ ๊ฒฝ์ฐ์ ์) + (i-1๋ฒ์งธ ์นธ์ ๊ฐ๋ ๊ฒฝ์ฐ์ ์)
4
+
5
+ Runtime: 0 ms (Beats: 100.00%)
6
+ Time Complexity: O(n)
7
+
8
+ Memory: 40.47 MB (Beats: 36.79%)
9
+ Space Complexity: O(1)
10
+ */
11
+
12
+ class Solution {
13
+ public int climbStairs (int n ) {
14
+ if (n == 1 ) {
15
+ return 1 ;
16
+ } else if (n == 2 ) {
17
+ return 2 ;
18
+ } else {
19
+ int prev2 = 1 ;
20
+ int prev1 = 2 ;
21
+ int cur = 0 ;
22
+ for (int i = 3 ; i <= n ; i ++) {
23
+ cur = prev2 + prev1 ;
24
+ prev2 = prev1 ;
25
+ prev1 = cur ;
26
+ }
27
+ return cur ;
28
+ }
29
+ }
30
+ }
You canโt perform that action at this time.
0 commit comments