We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fd9ab2b commit 7904a3cCopy full SHA for 7904a3c
climbing-stairs/ready-oun.java
@@ -0,0 +1,25 @@
1
+class Solution {
2
+ public int climbStairs(int n) {
3
+ if (n <= 2) return n; // base cases
4
+
5
+ int first = 1; // f(1)
6
+ int second = 2; // f(2)
7
8
+ // bottom-up dp: s to n
9
+ for (int i = 3; i <= n; i++) {
10
+ int current = first + second; // f(i) = f(i-1) + f(i-2)
11
+ first = second; //
12
+ second = current;
13
+ }
14
+ return second;
15
16
+}
17
18
+/**
19
+ Fibonacci-like DP problem -> f(n) = f(n-1) + f(n-2)
20
+ (n 번째 칸에 오르는 방법의 개수) = (n-1 번째 칸에 오르는 방법의 개수) + (n-2 번째 칸에 오르는 방법의 개수)
21
+ - Time Complexity: O(n)
22
+ loop from 3 to n once
23
+ - Space Complexity: O(1)
24
+ only 2 var are used
25
+ */
0 commit comments