当前位置:网站首页>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;
}
};
边栏推荐
- 工作中非常重要的测试策略,你大概没注意过吧
- Creation of volume group for AIX storage management (I)
- Leetcode skimming: stack and queue 01 (realizing queue with stack)
- Some understandings of graph convolution neural network r-gcn considering relations and some explanations of DGL official code
- 【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
- If the browser is accidentally closed, how does react cache the forms filled out by users?
- Synthetic watermelon game wechat applet source code / wechat game applet source code
- What skills does an excellent software tester need to master?
- What does open loop and closed loop mean?
- Global and Chinese markets for the application of artificial intelligence in security, public security and national security 2022-2028: Research Report on technology, participants, trends, market size
猜你喜欢

How to reflect and solve the problem of bird flight? Why are planes afraid of birds?
![[conference resources] the Third International Conference on Automation Science and Engineering in 2022 (jcase 2022)](/img/a6/a2afdf9e18255c9171f61bf074998b.png)
[conference resources] the Third International Conference on Automation Science and Engineering in 2022 (jcase 2022)

【会议资源】2022年第三届自动化科学与工程国际会议(JCASE 2022)

Powerful calendar wechat applet source code - support the main mode of doing more traffic

BPR (Bayesian personalized sorting)

Iclr2022 | spherenet and g-spherenet: autoregressive flow model for 3D molecular graph representation and molecular geometry generation

2022 high altitude installation, maintenance and removal of test question simulation test platform operation

Datawhale community blackboard newspaper (issue 1)

测试人进阶技能:单元测试报告应用指南

You probably haven't noticed the very important testing strategy in your work
随机推荐
【八大排序①】插入排序(直接插入排序、希尔排序)
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
@Valid parameter verification does not take effect
How does schedulerx help users solve the problem of distributed task scheduling?
Summary of Aix storage management
Leetcode skimming: binary tree 03 (post order traversal of binary tree)
Datawhale 社区黑板报(第1期)
Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
excel数据透视表
Global and Chinese markets for the application of artificial intelligence in security, public security and national security 2022-2028: Research Report on technology, participants, trends, market size
Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)
cookie、session、tooken
2022 high altitude installation, maintenance and removal of test question simulation test platform operation
Global and Chinese markets for distributed generation and energy storage in telecommunications networks 2022-2028: Research Report on technology, participants, trends, market size and share
[eight sorts ②] select sort (select sort, heap sort)
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
Picture puzzle wechat applet source code_ Support multi template production and traffic master
Some understandings of graph convolution neural network r-gcn considering relations and some explanations of DGL official code
[conference resources] the Third International Conference on Automation Science and Engineering in 2022 (jcase 2022)
[eight sorts ①] insert sort (direct insert sort, Hill sort)