class Solution {
public:
int fib(int N) {
if (N <= 1) return N;
vector<int> dp(N + 1);
// 初始条件
dp[0] = 0;
dp[1] = 1;
// 遍历顺序
for (int i = 2; i <= N; i++) {
// 状态转移
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[N];
}
};
class Solution {
public:
int climbStairs(int n) {
if(n == 1) return 1; // dp[1] = 1
int dp[46];
dp[2] = 2;
dp[3] = 3;
for(int i = 4; i <= n; i++){
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
};
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int dp[1001];
dp[0] = 0;
dp[1] = 0;
for(int i = 2; i <= cost.size(); i++){
dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]);
}
return dp[cost.size()];
}
};