问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4016 访问。

数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。

每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。

您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。

输入: cost = [10, 15, 20]

输出: 15

解释: 最低花费是从cost[1]开始,然后走两步即可到阶梯顶,一共花费15。

输入: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]

输出: 6

解释: 最低花费方式是从cost[0]开始,逐个经过那些1,跳过cost[3],一共花费6。

注意:

cost 的长度将会在 [2, 1000]。

每一个 cost[i] 将会是一个Integer类型,范围为 [0, 999]。


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.

Input: cost = [10, 15, 20]

Output: 15

Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

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:

cost will have a length in the range [2, 1000].

Every cost[i] will be an integer in the range [0, 999].


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4016 访问。

public class Program {

    public static void Main(string[] args) {
int[] cost = null; cost = new int[] { 10, 15, 20 };
var res = MinCostClimbingStairs(cost);
Console.WriteLine(res); cost = new int[] { 1, 100, 1, 1, 1, 100, 1, 1, 100, 1 };
res = MinCostClimbingStairs2(cost);
Console.WriteLine(res); Console.ReadKey();
} private static int MinCostClimbingStairs(int[] cost) {
int length = cost.Length + 1;
int[] step = new int[length];
step[0] = step[1] = 0;
for(int i = 2; i < length; i++) {
//每次选小往上撸
step[i] = Math.Min(step[i - 2] + cost[i - 2], step[i - 1] + cost[i - 1]);
}
return step[length - 1];
} private static int MinCostClimbingStairs2(int[] cost) {
//减少空间复杂度的解法,原理同上面的
int step1 = cost[0];
int step2 = cost[1];
int min = Math.Min(step1, step2);
for(int i = 2; i < cost.Length; i++) {
step2 = step1;
step1 = min + cost[i];
min = Math.Min(step1, step2);
}
return min;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4016 访问。

15
6

分析:

显而易见,以上2种算法的时间复杂度均为:  。

最新文章

  1. JAVA面试题
  2. Hive的三种安装方式(内嵌模式,本地模式远程模式)
  3. 首师大附中互测题:99999999海岛帝国后传:算法大会【D001】
  4. 移动web开发之屏幕三要素
  5. jqmobile小技巧
  6. JSP页面间传递参数的5种方法
  7. Java基础知识强化51:经典排序之桶排序(BucketSort)
  8. 一个简单的EXTJS案例
  9. jstring, String, char* 变换函数
  10. hihoCoder1310 岛屿 (dfs)
  11. 并发编程(一)—— volatile关键字和 atomic包
  12. 《Java 多线程编程核心技术》- 笔记
  13. mssql sqlserver 使用sql脚本获取群组后,按时间排序(asc)第一条数据的方法分享
  14. 自学Aruba集锦
  15. orcal 程序自动和手动项
  16. 001之IP基础对话框
  17. Linux之poll机制分析
  18. RabbitMQ--学习资源汇
  19. os.path等os模块函数
  20. Extjs下拉树代码测试总结

热门文章

  1. SpringCloud全家桶介绍及手绘架构
  2. 重磅分享:美团点评架构师私藏的内部Linux运维笔记
  3. k8s极简史:K8s多集群技术发展的历史、现状与未来
  4. 基于.Net Core的Redis实现查询附近的地理信息
  5. Azure Load Balancer(一) 为我们的Web项目提供负载均衡
  6. Maven&amp;mdash;&amp;mdash;软件开发中一个神奇的项目管理工具
  7. Linux最常用的基本操作复习
  8. PHP fgetcsv() 函数
  9. PHP xml_parse() 函数
  10. 4.13 省选模拟赛 传销组织 bitset 强连通分量 分块