public class Solution {
// f[i] = how many distinct ways from i to the top
// f[i] = f[i + 1] + f[i + 2]
public int climbStairs(int n) {
if (n < 2) {
return n;
}
int[] f = new int[n + 1];
f[n] = 0;
f[n - 1] = 1;
if (n >= 2) {
f[n - 2] = 2;
}
for (int i = n - 3; i >= 0; i--) {
f[i] = f[i + 1] + f[i + 2];
}
return f[0];
}
}
没有评论:
发表评论