• 746. Min Cost Climbing Stairs


    Problem:

    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].

    思路

    Solution (C++):

    int minCostClimbingStairs(vector<int>& cost) {
        int n = cost.size();
        if (n == 1)  return cost[0];
        vector<int> dp(n+1, 0);
        dp[0] = 0;
        dp[1] = 0;
        for (int i = 2; i <= n; ++i) {
            dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]);
        }
        return dp[n];
    }
    

    性能

    Runtime: 4 ms  Memory Usage: 6.8 MB

    思路

    Solution (C++):

    
    

    性能

    Runtime: ms  Memory Usage: MB

    相关链接如下:

    知乎:littledy

    欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

    作者:littledy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    Cmake Make makefile GNU autotools
    第三方库的安装:Pangolin
    ./configure, make, sudo make install 的含义
    [Eigen]C++开源线代库
    术语解释
    KDevelop使用笔记【中文教程】
    Python-Day1
    找不到或无法加载主类
    仅仅测试Word2016发布博客
    First Day!
  • 原文地址:https://www.cnblogs.com/dysjtu1995/p/12733344.html
Copyright © 2020-2023  润新知