题目地址:

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

解题思路:

官方给出的做法是倒着来,其实正着来也可以,无非就是进入某个step有两种方式,退出某一个

step也有两种方式,因此若用dp[i]表示进入第i步并预计从这里退出的最小值,dp[i] = cost[i] + min(dp[i-1],dp[i-2])

最后求退出时最小值,min(dp[i-1],dp[i])

真正写代码时不必用数组记录dp,用f1表示dp[i-2],f2表示dp[i-1].然后用dp[i-1],dp[i]更新f1,和f2

代码:

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int f1 = cost[];
int f2 = cost[];
for (size_t i = ; i < cost.size(); i++)
{
int f3 = cost.at(i) + min(f1, f2);
f1 = f2;
f2 = f3;
} return min(f1,f2);
}
};

【Leetcode】746. Min Cost Climbing Stairs的更多相关文章

  1. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  2. 【Leetcode_easy】746. Min Cost Climbing Stairs

    problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...

  3. 【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 ...

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

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

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

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

  8. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

  9. LeetCode算法题-Min Cost Climbing Stairs(Java实现)

    这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...

随机推荐

  1. Mysql 查看所有线程,被锁的表等

    ## 查看所有MYSQl相关的线程 > show full processlist; ## 杀死线程id为2的线程 > kill 2 ## 查看服务器状态 > show status ...

  2. 什么是epistatic effects | 上位效应

    epistatic与interaction之间的区别与联系? genetic上的interaction是如何定义的? Epistasis is the phenomenon where the eff ...

  3. oracle导入提示“IMP-00010:不是有效的导出文件,头部验证失败”的解决方案

    这是由于导出的dmp文件与导入的数据库的版本不同造成的用Notepad++查看了dmp文件,在头部具修改成你将导入目标数据库的版本号以下对应的版本号: 11g R2:V11.02.00 11g R1: ...

  4. Mosquitto配置----日志设置

    https://blog.csdn.net/u012377333/article/details/71101725 # ======================================== ...

  5. 小D课堂 - 零基础入门SpringBoot2.X到实战_第7节 SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf_28..SpringBoot Starter讲解

    笔记 1.SpringBoot Starter讲解     简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-b ...

  6. MacOS 安装配置 Laravel

    简单介绍: Laravel是一个用PHP编写的免费开源Web框架.它是由Taylor Otwell创作的,遵循MVC开发方法. 截至2015年3月,Laravel被认为是最流行的基于PHP的框架之一. ...

  7. shell编程系列8--文本处理三剑客之grep和egrep

    grep和egrep 第一种形式:grep [option] [pattern] [file1,file2...] 第二种形式:command | grep [option] [pattern] gr ...

  8. WebMercator和geographic互相转换

    方法1:esri的sdk中包含的方法:esri.geometry.geographicToWebMercator() 方法2:自己转换 //经纬度转Web墨卡托 function lonLat2Web ...

  9. 008-SpringBoot发布WAR启动报错:Error assembling WAR: webxml attribute is required

    一.Spring Boot发布war包流程: 1.修改web model的pom.xml <packaging>war</packaging> SpringBoot默认发布的都 ...

  10. Django Model 定义语法

    简单用法 from django.db import models class Person(models.Model): first_name = models.CharField(max_leng ...