File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ [๋ฌธ์ ํ์ด]
3
+ - 1 ๋๋ 2๋ก n์ ๋ง๋ค ์ ์๋ ์ ์ฒด ๊ฐ์ง์ ๊ตฌํ๊ธฐ
4
+ - dfs (X) ์๊ฐ์ด๊ณผ
5
+ class Solution {
6
+
7
+ public int climbStairs(int n) {
8
+ return dfs(n);
9
+ }
10
+
11
+ private int dfs(int n) {
12
+ if (n == 0) {
13
+ return 1;
14
+ }
15
+ if (n < 0) {
16
+ return 0;
17
+ }
18
+ return dfs(n - 1) + dfs(n - 2);
19
+ }
20
+ }
21
+
22
+ - DP (O)
23
+ time: O(N), space: O(1)
24
+
25
+ [ํ๊ณ ]
26
+ ๊ท์น์ ์ฐพ์ผ๋ ค๊ณ ํ์๋๋ฐ ๋ชป์ฐพ์๋ค..
27
+ ํผ๋ณด๋์น ์์ด.. ํ์๋ ๋ฌธ์ ์ธ๋ฐ.. ์๊ฐํ์;
28
+ F(N) = F(n-1) + F(n-2)
29
+ */
30
+ class Solution {
31
+
32
+ public int climbStairs (int n ) {
33
+ if (n <= 3 ) {
34
+ return n ;
35
+ }
36
+
37
+ int prev1 = 3 ;
38
+ int prev2 = 2 ;
39
+ int current = 0 ;
40
+ for (int i = 4 ; i <= n ; i ++) {
41
+ current = prev1 + prev2 ;
42
+ prev2 = prev1 ;
43
+ prev1 = current ;
44
+ }
45
+ return current ;
46
+ }
47
+ }
You canโt perform that action at this time.
0 commit comments