题解 | #最小花费爬楼梯#
最小花费爬楼梯
https://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <map> #include <queue> #include <cmath> using namespace std; int cost[100005]; int dp[100005]; //dp[i] 到达第i层时,所花费的最小值 int main() { int n; while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%d", &cost[i]); dp[i] = cost[i]; } dp[0] = 0; dp[1] = 0; //从0层和1层都可以开始,花费为0 for (int i = 2; i <= n; i++) { dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); } printf("%d\n", dp[n]); } }