当前位置: 首页 > article >正文

LeetCode746. Min Cost Climbing Stairs

文章目录

    • 一、题目
    • 二、题解

一、题目

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

Example 1:

Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.

  • Pay 15 and climb two steps to reach the top.
    The total cost is 15.
    Example 2:

Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.

  • Pay 1 and climb two steps to reach index 2.
  • Pay 1 and climb two steps to reach index 4.
  • Pay 1 and climb two steps to reach index 6.
  • Pay 1 and climb one step to reach index 7.
  • Pay 1 and climb two steps to reach index 9.
  • Pay 1 and climb one step to reach the top.
    The total cost is 6.

Constraints:

2 <= cost.length <= 1000
0 <= cost[i] <= 999

二、题解

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

http://www.kler.cn/a/147329.html

相关文章:

  • OpenHarmony-1.启动流程
  • 职场汇报技巧:选择合适的汇报形式与提供数据依据
  • 15-1.Java 网络编程之 InetAddress(InetAddress 常用静态方法、InetAddress 常用方法)
  • 【Apache Paimon】-- 1 -- Apache Paimon 是什么?
  • Python教程笔记(2)
  • ubuntu16.04配置网卡
  • Python---引用变量与可变、非可变类型
  • Ubuntu 22.03 LTS 安装deepin-terminal 实现 终端 分屏
  • 笔记二十一、使用路由search进行传递参数
  • pandas教程:时区计数 USA.gov Data from Bitly USA.gov数据集
  • 反爬虫机制与反爬虫技术(二)
  • 【ISP】噪声--sensor(2)
  • DS二叉树--赫夫曼树解码/最优二叉树【数据结构】
  • acwing算法基础之动态规划--背包问题
  • 【服务器能干什么】搭建一个短网址平台,可以查看数据详情!
  • Java的buffer.flip()是个什么东西?
  • XML Schema中的attributeFormDefault
  • el-row el-col书写不规范的问题,会换行
  • 奖学金问题
  • Ansible的module_defaults
  • 深兰科技“汉境”入选2023年湖北省人工智能十大优秀应用案例
  • 实时云渲染简要分析
  • Python爬虫入门课: 如何实现数据抓取 <文字 图片 音频 视频 文档..>
  • 通过JDBC连接MySQL实现表的插入和查看语句
  • IDEA断点调试
  • 双通道 12V 直流电机驱动芯片GC8548,12V,大电流,具有短地短电源保护功能,可替代LV8548/LV8549/ONSEMI