当前位置:网站首页>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}));
}
}

边栏推荐
- 科目1-2
- What does CRM mean? Three "key points" for CRM management software selection
- UE5影视动画渲染MRQ分层学习笔记
- Scheme and software analysis of dual computer hot standby system "suggestions collection"
- [don't bother to strengthen learning] video notes (IV) 2. Dqn realizes maze walking
- js定位大全获取节点的兄弟,父级,子级元素含robot实例
- Problems and abuse of protocol buffers
- Why is TCP a triple handshake
- Leetcode94-二叉树的中序遍历详解
- Getting started with web security - open source firewall pfsense installation configuration
猜你喜欢

xtrabackup 实现mysql的全量备份与增量备份

Attack and defense world ----- confusion1

《动手学深度学习》(七) -- 边界框和锚框

链表——19. 删除链表的倒数第 N 个结点

MySQL Basics (I) -- SQL Basics

Xtrabackup realizes full backup and incremental backup of MySQL

Racecar multi-point navigation experiment based on ROS communication mechanism

【我的创作一周年纪念日】爱情是需要被纪念的,创作也是
![[the first anniversary of my creation] love needs to be commemorated, so does creation](/img/89/2f8eec4f0a0bcf77d5a91179012899.png)
[the first anniversary of my creation] love needs to be commemorated, so does creation

One year after I came to Ali, I ushered in my first job change
随机推荐
云原生(十二) | Kubernetes篇之Kubernetes基础入门
Open source summer interview | learn with problems, Apache dolphin scheduler, Wang Fuzheng
We were tossed all night by a Kong performance bug
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
js定位大全获取节点的兄弟,父级,子级元素含robot实例
Re6: reading paper licin: a heterogeneous graph based approach for automatic legal stat identification fro
Huawei wireless device security policy configuration command
Aruba学习笔记06-无线控制AC基础配置(CLI)
How to judge and analyze NFT market briefly through NFT go?
xtrabackup 实现mysql的全量备份与增量备份
2021 robocom world robot developer competition - undergraduate group (Preliminary) problem solution
TCP triple handshake connection combing
CodeBlocks shortcut key operation Xiaoquan
Problem: filesystemversionexception: you have version null and I want version 8
Racecar multi-point navigation experiment based on ROS communication mechanism
Tiktok shop will add a new site, and the Singapore site will be launched on June 9
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
《动手学深度学习》(七) -- 边界框和锚框
Virtual machine terminator terminal terminator installation tutorial
Linked list - 19. Delete the penultimate node of the linked list