当前位置:网站首页>Dynamic planning - Taking stair climbing with minimum cost as an example
Dynamic planning - Taking stair climbing with minimum cost as an example
2022-06-22 03:41:00 【Yu who wants to fly】
Dynamic programming - Take stair climbing with minimum cost as an example
The title describes each subscript of the array as a ladder , The first i A ladder corresponds to a non negative amount of physical expenditure cost[i]( Subscript from 0 Start ).
Every time you climb a ladder, you have to spend the corresponding stamina value , Once you've paid for your strength , You can choose to climb up one ladder or two .
Please find out the lowest cost to reach the top of the floor . At the beginning , You can choose from the subscript to 0 or 1 As the initial step .

determine dp Array and subscript meaning
Observe cost Array understanding question stem ,cost[i] yes Climb up The first i The number of physical strength to spend , Every time I climb i, Must be from i-1, perhaps i-2 Climb up , newly build dp Array
int[] dp = new int[cost.length+1];
Determine the recurrence formula
Observation found that , Give Way dp[i] Corresponding climb cost[i-1] Physical strength expended
dp[i]=Math.min(dp[i-2]+cost[i-2],dp[i-1]+cost[i-1]);
dp Array initialization
Every dp[i] By the previous dp[i-1] and dp[i-2] By comparison
dp[0]=0;
dp[1]=0;
Determine the traversal order
From the subscript 2 Start , Traverse in turn
Complete code
public int minCostClimbingStairs(int[] cost) {
int length = cost.length;
int[] dp = new int[cost.length+1];
dp[0]= 0;
dp[1]= 0;
for(int i=2;i<=cost.length;++i){
dp[i]=Math.min(dp[i-2]+cost[i-2],dp[i-1]+cost[i-1]);
}
return dp[length];
}
边栏推荐
- c# 自定义排序
- A component required a bean of type 'com. example. demo3.service. UserServiceImp' that could not be fou
- 1299. replace each element with the largest element on the right
- Based on logback XML to realize the insensitive operation of saving log information
- 泛微 E-cology V9 信息泄露漏洞
- Sword finger offer 68 - I. nearest common ancestor of binary search tree
- Nepal graph learning Chapter 2_ A bug before version v2.6.1 caused OOM
- 聊聊flink水位线
- svn与cvs的区别有哪些
- 倍福TwinCAT3控制器和控制器间的Ads通讯
猜你喜欢

Talk about the Flink waterline

试用了多款报表工具,终于找到了基于.Net 6开发的一个了

【Leetcode】17回溯(电话号码的字母组合)

Contact and understanding in PowerDesigner CDM

倍福TwinCAT3第三方伺服电机——以汇川IS620N伺服为例子

未來已來:雲原生時代

AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了

基于51的超声波测距仪代码(截图版)

VIM from dislike to dependence (18) -- advanced search mode

我们如何解决了RealSense偏色问题?
随机推荐
1690. stone game vii- dynamic programming method
云原生架构(02)-什么是云原生
Zombie process and orphan process
Summary of image classification based on pytoch: swing transformer
Select in golang concurrent programming
(problem solving) missing gcr io/kubebuilder/kube-rbac-proxy:v0.8.0
试用了多款报表工具,终于找到了基于.Net 6开发的一个了
平衡二叉树——调整变换规则
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Blazor University (31)表单 —— 验证
Sword finger offer 68 - ii Nearest common ancestor of binary tree
Pointer and pointer of pointer
内网穿透
Template as interface
DM达梦数据的关键字与表的字段冲突的解决办法
倍福TwinCAT3伺服控制常用功能块的实现
TwinCAT 3 RS232通信的关键程序
Introduction and use of kubernetes
Use yolov5 to train your own data set; Installation and use of yolov5; Interpretation of yolov5 source code
C51的一些日记