当前位置:网站首页>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;
}
};
边栏推荐
- 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
- 工作中非常重要的测试策略,你大概没注意过吧
- RFID让固定资产盘点更快更准
- Global and Chinese markets for supply chain strategy and operation consulting 2022-2028: Research Report on technology, participants, trends, market size and share
- Mathematics - feelings -20220215
- Advanced skills of testers: a guide to the application of unit test reports
- Deb file installation
- sso单点登录的实现。
- Variables and constants of go language foundation
- Creating logical volumes and viewing and modifying attributes for AIX storage management
猜你喜欢

Leetcode question brushing: stack and queue 07 (maximum value of sliding window)

XMind思维导图

Xinniuniu blind box wechat applet source code_ Support flow realization, with complete material pictures

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

首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!

Review notes of compilation principles

Leetcode skimming: binary tree 01 (preorder traversal of binary tree)

【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能

Collection: comprehensive summary of storage knowledge

The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
随机推荐
Variables and constants of go language foundation
How to determine whether the current script is in the node environment or the browser environment?
Sql--- related transactions
You probably haven't noticed the very important testing strategy in your work
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
什么是商业养老保险?商业养老保险安全靠谱吗?
MySQL winter vacation self-study 2022 12 (4)
Barbie q! How to analyze the new game app?
Node - generate wechat permission verification configuration
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
King combat power query renamed toolbox applet source code - with traffic main incentive advertisement
BiLSTM-CRF代码实现
Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
CEPH buffer yyds dry inventory
Cat Party (Easy Edition)
Mitsubishi PLC FX3U pulse axis jog function block (mc_jog)
【八大排序②】选择排序(选择排序,堆排序)
How does schedulerx help users solve the problem of distributed task scheduling?
Leetcode question brushing: stack and queue 07 (maximum value of sliding window)