算法题目--Min Cost Climbing Stairs

题目来源

https://leetcode.com/problems/min-cost-climbing-stairs/description/

题目描述

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).

Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].

Note:

  1. cost will have a length in the range [2, 1000].
  2. Every cost[i] will be an integer in the range [0, 999].

心路历程

使用动态规划解决。从序列后面开始往前计算最小代价。比如:

1, 100, 1, 1, 1, 100, 1, 1, 100, 1可以看成:

1, 100, 1, 1, 1, 100, 1, 1, 100, 1,0,0

初始化right1,right2为0,表示最右面两个台阶的代价为0。往前看是1,这时候假设已经到达这一台阶,有两种选择,因此选择小的一种(此处都是0)。继续往前看,此时right1和right2也都往前移了一步,right1等于1,right1等于之前的right1(0)。好了,再看现在的100,同样有两种选择,继续选择小的。再往前一步,right1变成100,right2变成1……重复此操作,直到最前面。

解决方案

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int length = cost.size();
        int right1 = 0;
        int right2 = 0;
        for (int i = length - 1; i >= 0; i--) {
            int cur = cost[i] + min(right1, right2);
            right2 = rigth1;
            right1 = cur;
        }
        return min(right1, right2);
    }
};
int main() {
    Solution solution;
    vector<int> test1 = { 1, 100, 1, 1, 1, 100, 1, 1, 100, 1 };
    cout << solution.minCostClimbingStairs(test1) << endl;
    vector<int> test2 = { 10,15,20};
    cout << solution.minCostClimbingStairs(test2) << endl;
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值