[LC] 746. Min Cost Climbing Stairs
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].
class Solution {
public int minCostClimbingStairs(int[] cost) {
if (cost.length == 1) {
return cost[0];
}
if (cost.length == 2) {
return Math.min(cost[0], cost[1]);
}
int cur = 0, res = 0;
int twoBefore = cost[0], oneBefore = cost[1];
for (int i = 2; i < cost.length; i++) {
cur = Math.min(twoBefore, oneBefore) + cost[i];
twoBefore = oneBefore;
oneBefore = cur;
}
return Math.min(twoBefore, oneBefore);
}
}
[LC] 746. Min Cost Climbing Stairs的更多相关文章
- LN : leetcode 746 Min Cost Climbing Stairs
lc 746 Min Cost Climbing Stairs 746 Min Cost Climbing Stairs On a staircase, the i-th step has some ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- 746. Min Cost Climbing Stairs@python
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
- 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯
[抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...
- 【easy】746. Min Cost Climbing Stairs 动态规划
On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you pay t ...
- [LeetCode&Python] Problem 746. Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
随机推荐
- Linux CMD 笔记 & 正则表达式
一.linux bash 1. 进程名查找kill ps -ef | grep xxxx| grep -v grep| cut -c 9-15 | xargs kill -9 2.端口号kill 占用 ...
- JavaScript学习总结(五)
之前的几讲中我们曾经说过,JavaScript中是没有类的概念的.但是我们讲过对象,那么这个对象是怎么来的呢? 只要有函数即可创建对象 自定义对象 自定义对象的方式: 1. 使用无参的函数创建对象 & ...
- 学习Unet的一些过程
毕业设计的项目,需要用到手机作为控制端,这就需要用到Unity的网络模块. 因为只会设计到几个简单的按钮命令,所以不打算做多么复杂的功能,一开始打算用C#的Socket编程,但是考虑到多线程的关系觉得 ...
- luogu P3835 【模板】可持久化平衡树
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> usin ...
- 中小规模集群----Centos6部署wordpress及java程序
1 概述 1.1 业务需求 公司共有两个业务,网上图书馆和一个电商网站.现要求运维设计一个安全架构,本着高可用.廉价的原则. 具体情况如下: 网上图书馆是基于jsp开发: 电商系统是基于 ...
- 68)deque数组
基本要求: 1)和vecctor基本区别 示意图 vector在尾部添加和删除, deque在尾部添加和删除,在头部添加和删除. 2)基本知识: 3)deque的构造形式: 4)基本操作和遍历 ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- Java常用面试题总结
1.多线程实现方式 2.cookie和session区别 3.数据加密 4.接口并发 5.常用的集合类 6.遍历集合方式 7.接口和抽象类 8.#和$区别 9.防止sql注入 10.springMvc ...
- 在CentOS7上从源码编译安装redis,并做成服务程序
1.安装编译的依赖环境 # 安装pcre开发包: yum install -y pcre-devel # 安装ssl功能需要openssl库 yum -y install openssl-devel ...
- pyinstaller 3.6版本通过pip安装失败的解决办法
本机中原pyinstaller版本为3.5版本,本打算通过 pip install --upgrade pyinstaller进行升级,竟然报错,后面卸载再重新安装也一样报错,没办法看来通过pip是暂 ...