当前位置:网站首页>Leetcode 45 Jumping game II (2022.02.14)
Leetcode 45 Jumping game II (2022.02.14)
2022-07-02 01:07:00 【ChaoYue_ miku】
Here's an array of nonnegative integers nums , You are first in the array .
Each element in the array represents the maximum length you can jump at that location .
Your goal is to reach the last position of the array with the least number of jumps .
Suppose you can always reach the last position of the array .
Example 1:
Input : nums = [2,3,1,1,4]
Output : 2
explain : The minimum number of jumps to the last position is 2.
From the subscript for 0 Jump to subscript 1 The location of , jump 1 Step , Then jump 3 Step to the last position of the array .
Example 2:
Input : nums = [2,3,0,1,4]
Output : 2
Tips :
1 <= nums.length <= 104
0 <= nums[i] <= 1000
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/jump-game-ii
Method 1 : Greedy Algorithm
C++ Submission :
class Solution {
public:
int jump(vector<int>& nums)
{
int ans = 0;
int end = 0;
int maxPos = 0;
for (int i = 0; i < nums.size() - 1; i++)
{
maxPos = max(nums[i] + i, maxPos);
if (i == end)
{
end = maxPos;
ans++;
}
}
return ans;
}
};
边栏推荐
- 教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
- Export default the exported object cannot be deconstructed, and module Differences between exports
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- excel查找与引用函数
- PLC Analog input analog conversion FB s_ ITR (Mitsubishi FX3U)
- How to determine whether the current script is in the node environment or the browser environment?
- 【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
- Friends circle community program source code sharing
- [leetcode] number of maximum consecutive ones
- Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
猜你喜欢
随机推荐
What skills does an excellent software tester need to master?
AIX存储管理之卷组的创建(一)
Otaku wallpaper Daquan wechat applet source code - with dynamic wallpaper to support a variety of traffic owners
cookie、session、tooken
Global and Chinese market of wireless chipsets 2022-2028: Research Report on technology, participants, trends, market size and share
AIX存储管理之卷组属性的查看和修改(二)
[JS download files through url]
@Valid参数校验不生效
Iclr2022 | spherenet and g-spherenet: autoregressive flow model for 3D molecular graph representation and molecular geometry generation
Variables and constants of go language foundation
New version of free mobile phone, PC, tablet, notebook four terminal Website thumbnail display diagram online one click to generate website source code
DTL dephossite | prediction method of dephosphorylation sites based on Transfer Learning
CEPH buffer yyds dry inventory
Weather forecast applet source code weather wechat applet source code
Powerful calendar wechat applet source code - support the main mode of doing more traffic
What is commercial endowment insurance? Is commercial endowment insurance safe?
Leetcode skimming: stack and queue 04 (delete all adjacent duplicates in the string)
cookie、session、tooken
Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)
Cookie, session, tooken





![[dynamic planning] interval dp:p3205 Chorus](/img/25/3dc7132e1aaa5c0eca87382692fc12.jpg)


