https://leetcode.com/problems/climbing-stairs/
- 1D DP
Each step can be reached from the previous one or the one before that, so compute the Fibonacci-style recurrence.
O(n)
O(1)
class Solution {
public int climbStairs(int n) {
if (n <= 2) return n;
int first = 1, second = 2;
for (int i = 3; i <= n; i++) {
int current = first + second;
first = second;
second = current;
}
return second;
}
}