当前位置:网站首页>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
边栏推荐
- 1349. 参加考试的最大学生数 状态压缩
- iNFTnews | What can NFTs bring to the sports industry and fans?
- 【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
- Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
- 亚马逊云科技携手中科创达为行业客户构建AIoT平台
- Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant
- js中try...catch和finally的用法
- 学习笔记-----左偏树
- 转:查尔斯·汉迪:你是谁,比你做什么更重要
- 在这个超连接的世界里,你的数据安全吗
猜你喜欢
Using OpenVINO to implement the flying paddle version of the PGNet inference program
刷爆朋友圈,Alibaba出品亿级并发设计速成笔记太香了
如何基于OpenVINO POT工具简单实现对模型的量化压缩
优化Feed流遭遇拦路虎,是谁帮百度打破了“内存墙”?
1349. Maximum number of students taking the exam Status Compression
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
《.NET物联网从零开始》系列
编译预处理等细节
IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
随机推荐
ARM Mailbox
MySQL学习
记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
js中try...catch和finally的用法
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
Opening - Open a new .NET modern application development experience
汇编语言之源程序
.Net C# Console Create a window using Win32 API
Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
MySQL3
树形查找(二叉查找树)
亚马逊云科技携手中科创达为行业客户构建AIoT平台
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
hypervisor相关的知识点
力扣-相同的树
短域名绕过及xss相关知识
回顾51单片机
[parameters of PyQT5 binding functions]