当前位置:网站首页>Leetcode skimming: dynamic planning 03 (climb stairs with minimum cost)
Leetcode skimming: dynamic planning 03 (climb stairs with minimum cost)
2022-07-24 09:22:00 【Taotao can't learn English】
746. Use the minimum cost to climb the stairs
Each subscript of the array acts 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 .
Example 1:
Input :cost = [10, 15, 20]
Output :15
explain : The minimum cost is from cost[1] Start , Then take two steps to the top of the steps , Total cost 15 .
Example 2:
Input :cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output :6
explain : The lowest cost way is from cost[0] Start , One by one through those 1 , skip cost[3] , Total cost 6 .
Tips :
- cost The length range of is [2, 1000].
- cost[i] It's going to be an integer , The scope is [0, 999] .
Just think of this question dp The meaning of each layer , It's easy to do it
dp[i] For the minimum consumption of jumping out of the current layer .
dp[i]=dp[i-1]+dp[i-2]+cost[i]
package com.programmercarl.dynamic;
/** * @ClassName MinCostClimbingStairs * @Descriotion TODO * @Author nitaotao * @Date 2022/7/22 12:34 * @Version 1.0 * https://leetcode.cn/problems/min-cost-climbing-stairs/ * 746. Use the minimum cost to climb the stairs **/
public class MinCostClimbingStairs {
public int minCostClimbingStairs(int[] cost) {
// Choose two positions at a time and add the cheapest one in one step
// Jumping out of this level requires the least consumption
int[] dp = new int[cost.length];
dp[0] = cost[0];
dp[1] = cost[1];
for (int i = 2; i < dp.length; i++) {
// State transition equation
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];
}
return Math.min(dp[cost.length - 1], dp[cost.length - 2]);
}
public static void main(String[] args) {
System.out.println(new MinCostClimbingStairs().minCostClimbingStairs(new int[]{
0, 0, 1, 1}));
System.out.println(new MinCostClimbingStairs().minCostClimbingStairs(new int[]{
1,100,1,1,1,100,1,1,100,1}));
}
}

边栏推荐
- A null pointer exception is reported when the wrapper class inserts into the empty field of the database table
- Aruba学习笔记06-无线控制AC基础配置(CLI)
- (5) Cloud integrated gateway gateway +swagger documentation tool
- Replace the function of pow with two-dimensional array (solve the time overrun caused by POW)
- Code random notes_ Linked list_ Turn over the linked list in groups of 25K
- Ansible 常用模块介绍
- Will your NFT disappear? Dfinity provides the best solution for NFT storage
- Scheme and software analysis of dual computer hot standby system "suggestions collection"
- [don't bother to strengthen learning] video notes (II) 2. Write a small example of Q learning
- How should tiktok shop cooperate with live broadcast in the background?
猜你喜欢

Little dolphin "transformed" into a new intelligent scheduling engine, which can be explained in simple terms in the practical development and application of DDS

Tiktok's "online celebrity" was poached by Amazon and broadcast on Amazon live platform

Tiflash source code reading (V) deltatree storage engine design and implementation analysis - Part 2
![[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)](/img/3b/981bd564a5855a317ccdd4800871ce.png)
[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)

Detailed explanation of the whole process of R & D demand splitting | agile practice

Account 1-3

Android system security - 5.2-apk V1 signature introduction

【基于ROS的URDF练习实例】四轮机器人与摄像头的使用

OPENCV学习DAY5

Getting started with web security - open source firewall pfsense installation configuration
随机推荐
[the first anniversary of my creation] love needs to be commemorated, so does creation
Guys, what parameters can be set when printing flinksql so that the values can be printed? This later section is omitted. It's inconvenient. I read the configuration on the official website
Code random notes_ Linked list_ Turn over the linked list in groups of 25K
Promise basic summary
Huawei wireless device security policy configuration command
Tiktok live broadcast with goods marketing play
How to judge and analyze NFT market briefly through NFT go?
【汇编语言实战】一元二次方程ax2+bx+c=0求解(含源码与过程截屏,可修改参数)
来阿里一年后我迎来了第一次工作变动....
Linked list - 19. Delete the penultimate node of the linked list
Let's test 5million pieces of data. How to use index acceleration reasonably?
[example of URDF exercise based on ROS] use of four wheeled robot and camera
UE5影视动画渲染MRQ分层学习笔记
Makefile variables and dynamic library static library
[assembly language practice] (II). Write a program to calculate the value of expression w=v- (x+y+z-51) (including code and process screenshots)
Protocol buffers 的问题和滥用
js定位大全获取节点的兄弟,父级,子级元素含robot实例
Re6: reading paper licin: a heterogeneous graph based approach for automatic legal stat identification fro
Getting started with web security - open source firewall pfsense installation configuration
[Luogu p3426] SZA template (string) (KMP)