Skip to content

Commit 1cfbf82

Browse files
committed
climbing-stairs
1 parent 34ec223 commit 1cfbf82

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

climbing-stairs/hoonjichoi1.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/*
3+
f(45) = f(44) + f(43)
4+
f(44) = f(43) + f(42)
5+
.
6+
.
7+
.
8+
f(3) = f(2) + f(1)
9+
f(2) = 2
10+
f(1) = 1
11+
*/
12+
class Solution {
13+
public int climbStairs(int n) {
14+
if (n <= 2) return n;
15+
16+
int prev = 1;
17+
int result = 2;
18+
int temp = 0;
19+
for (int i = 2; i < n ; i++) {
20+
temp = result;
21+
result += prev;
22+
prev = temp;
23+
}
24+
return result;
25+
}
26+
}

0 commit comments

Comments
 (0)