public class Solution {
public int climbStairs(int n) {
// Fibonacci
// climbStairs(n) = climbStairs(n - 1) + climbStairs(n - 2)
int prev = 1, curr = 1;
for(int i = 2; i <= n; i++) {
int tmp = prev + curr;
prev = curr;
curr = tmp;
}
return curr;
}
}
Time: O(n)
Space: O(1)