当前位置:网站首页>LeetCode uses the minimum cost to climb the stairs----dp problem
LeetCode uses the minimum cost to climb the stairs----dp problem
2022-08-05 02:18:00 【zero rain z】
Title: Give you an integer array cost, where cost[i] is the cost to climb up the first step of the stairs.Once you pay this fee, you can choose to climb one or two steps up.
You can choose to start the stairs from the steps with index 0 or 1.Please calculate and return the minimum cost to reach the top of the stairs.
Example:

This question is similar to the frog climbing the stairs, you can refer to the common thinking steps of the dp problem
Three steps for a dynamic programming problem:
First step: Define the meaning of the array elements
Second step: Find relationships between array elements
Step 3: Find the initial value
A general idea written in a drawing, with examples
dp[i] represents the minimum cost to reach i
cost[i] represents the cost to climb the i-th stair
i-1 stairs, cost[i-1] to reach subscript i
When 2 The code is as follows: This question can also be found through analysis that each element is only related to the previous element and the previous element, and you can think about whether it conforms to the scrolling array.
Example: when i is equal to 2, to
There are two ways to reach 2
Subscript O goes two steps or subscript 1 goes one step
dpli] means the minimum cost for you to go to the 0th or 1st order, because these twoThe step is the initial step and can be selected, so dp[1/0] is O, only need to consider cost[0] and cost[1] who is bigger. Both cost[0] and cost[1] can reach the second by paying the costThe number written on the step is the set cost, then it is the minimum of 0+2 and O+1 to know dp2=1
Similarly, when i=3, you can go through the first step to 2 or the second step to 1That is, 0+1 and 1+3 take the minimum value to know that dp3 = 1
until it reaches the top, that is, the end of the dp array, and returns to the end of dp.class Solution {public int minCostClimbingStairs(int[] cost) {int n = cost.length;int[] dp = new int[n + 1];dp[0] = dp[1] = 0;for (int i = 2; i <= n; i++) {dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);} }return dp[n];} }}class Solution {public:int minCostClimbingStairs(vector
边栏推荐
- Oracle encapsulates restful interfaces into views
- dotnet 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
- iNFTnews | What can NFTs bring to the sports industry and fans?
- 【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
- 力扣-二叉树的前序遍历、中序遍历、后序遍历
- KingbaseES V8 GIS数据迁移方案(2. Kingbase GIS能力介绍)
- Transfer Learning - Distant Domain Transfer Learning
- Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
- 迁移学习——Distant Domain Transfer Learning
- <开发>实用工具
猜你喜欢

一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征

sql语句多字段多个值如何进行排序

Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec

Opening - Open a new .NET modern application development experience

CPDA|运营人如何从负基础学会数据分析(SQL)

MySQL learning

Tree search (bintree)

ExcelPatternTool: Excel表格-数据库互导工具

01 【前言 基础使用 核心概念】

Leetcode刷题——22. 括号生成
随机推荐
重新审视分布式系统:永远不会有完美的一致性方案……
sql语句多字段多个值如何进行排序
js中try...catch和finally的用法
leetcode-另一棵树的子树
EBS利用虚拟列及hint 提示优化sql案例一则
树表的查找
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
EBS uses virtual columns and hint hints to optimize sql case
Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
[Endnote] Word inserts a custom form of Endnote document format
.Net C# Console Create a window using Win32 API
海量服务实例动态化管理
Hypervisor related knowledge points
select 标签自定义样式
Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
短域名绕过及xss相关知识
MySQL learning
多线程(2)