当前位置:网站首页>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;
}
};
边栏推荐
- UDS bootloader of s32kxxx bootloader
- Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
- 2022 safety officer-b certificate examination practice questions simulated examination platform operation
- [eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
- Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
- [WesternCTF2018]shrine writeup
- Source code of Qiwei automatic card issuing system
- How does schedulerx help users solve the problem of distributed task scheduling?
- AIX存储管理之卷组的创建(一)
- Leetcode skimming: stack and queue 02 (realizing stack with queue)
猜你喜欢

AIX存储管理之逻辑卷的创建及属性的查看和修改

Entrepreneurship is a little risky. Read the data and do a business analysis

Datawhale community blackboard newspaper (issue 1)

excel查找与引用函数
![[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)

New version of free mobile phone, PC, tablet, notebook four terminal Website thumbnail display diagram online one click to generate website source code

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

【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
![[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login](/img/c1/23be4399119f42d85a7b86fc8a59fc.png)
[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login

How do Lenovo computers connect Bluetooth headsets?
随机推荐
一名优秀的软件测试人员,需要掌握哪些技能?
Infiltration records of CFS shooting range in the fourth phase of the western regions' Dadu Mansion
[JS download files through url]
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
Global and Chinese markets for supply chain strategy and operation consulting 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
Cat Party (Easy Edition)
[dynamic planning] interval dp:p3205 Chorus
cookie、session、tooken
JMeter做接口测试,如何提取登录Cookie
Node - generate wechat permission verification configuration
XMIND mind map
Bc35 & bc95 onenet mqtt (old)
Some understandings of graph convolution neural network r-gcn considering relations and some explanations of DGL official code
cookie、session、tooken
Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
You probably haven't noticed the very important testing strategy in your work
SQL injection for Web Security (2)
【八大排序①】插入排序(直接插入排序、希尔排序)