Climbing Stairs
LeetCode https://leetcode.cn/problems/climbing-stairs/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int climbStairs(int n) {
if (n < 3) return n;
int[] memo = new int[n];
memo[0] = 1;
memo[1] = 2;
for (int i = 2; i < n; ++i) {
memo[i] = memo[i - 1] + memo[i - 2];
}
return memo[n - 1];
}
}
Complexity
- Time = O(n)
- Space = O(1)
This post is licensed under CC BY 4.0 by the author.