当前位置:网站首页>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
边栏推荐
- 【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
- leetcode 15
- Transfer Learning - Distant Domain Transfer Learning
- 02 【开发服务器 资源模块】
- Opening - Open a new .NET modern application development experience
- 迅睿cms网站搬迁换了服务器后网站不能正常显示
- 【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
Utilities - Programmer's list of sheep counting when insomnia | Daily anecdote
- How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
猜你喜欢
如何逐步执行数据风险评估
线性表的查找
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能
1349. Maximum number of students taking the exam Status Compression
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
关于#sql shell#的问题,如何解决?
Transfer Learning - Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
随机推荐
std::string::find 返回值的坑
ExcelPatternTool: Excel表格-数据库互导工具
开篇-开启全新的.NET现代应用开发体验
Transfer Learning - Distant Domain Transfer Learning
《.NET物联网从零开始》系列
MySQL learning
PHP Skills Assessment
迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
CMS建站流程
LPQ (local phase quantization) study notes
转:查尔斯·汉迪:你是谁,比你做什么更重要
iNFTnews | What can NFTs bring to the sports industry and fans?
[Word] #() error occurs after Word formula is exported to PDF
Tree search (bintree)
".NET IoT from scratch" series
海量服务实例动态化管理
Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]
高数_复习_第1章:函数、极限、连续
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
LPQ(局部相位量化)学习笔记