Skip to content

Commit 3ea1958

Browse files
committed
solve climbing stairs
1 parent 20dc9f6 commit 3ea1958

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

climbing-stairs/sora0319.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
// 공간 복잡도를 줄인 2번째 버전
2+
class Solution {
3+
public int climbStairs(int n) {
4+
int back = 1;
5+
int front = 2;
6+
int target = 0;
7+
8+
if (n == 1) return back;
9+
if (n == 2) return front;
10+
11+
for(int i = 1; i < n-1; i++){
12+
target = front + back;
13+
back = front;
14+
front = target;
15+
}
16+
17+
return target;
18+
}
19+
}
20+
21+
22+
// 초기 버전
123
class Solution {
224
public int climbStairs(int n) {
325
int[] stairs = new int[n+1];
@@ -12,3 +34,8 @@ public int climbStairs(int n) {
1234
}
1335
}
1436

37+
// 1 : 1
38+
// 2 : [1] + 1, 2 1+1 2 2개
39+
// 3 : [2-1] + 1, [2-2] + 1, [1] + 2 1+1+1 2+1 1+2 3개
40+
// 4 : [3-1] + 1, [3-2] + 1, [3-3] + 1 1+1+1+1 2+1+1 1+2+1 1+1+2 2+2 5개
41+
// 5 : 1+1+1+1+1 2+1+1+1 1+2+1+1 1+1+2+1 1+1+1+2 2+2+1 2+1+2 1+2+2 8개

0 commit comments

Comments
 (0)