当前位置:网站首页>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;
}
};
边栏推荐
- Kuberntes cloud native combat high availability deployment architecture
- RFID让固定资产盘点更快更准
- Node - generate wechat permission verification configuration
- gradle
- 【八大排序①】插入排序(直接插入排序、希尔排序)
- Leetcode skimming: stack and queue 06 (top k high-frequency elements)
- Entrepreneurship is a little risky. Read the data and do a business analysis
- 2022 low voltage electrician examination questions and answers
- How does schedulerx help users solve the problem of distributed task scheduling?
- Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
猜你喜欢

Leetcode skimming: stack and queue 03 (valid parentheses)

Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master

Weather forecast applet source code weather wechat applet source code

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

JMeter做接口测试,如何提取登录Cookie

JS -- image to base code, base to file object

gradle

【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)

The pain of Xiao Sha
![[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)](/img/0d/22f3f65ab9422383df9a55d0724d59.jpg)
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
随机推荐
What does open loop and closed loop mean?
You probably haven't noticed the very important testing strategy in your work
Excel search and reference function
MySQL winter vacation self-study 2022 12 (4)
ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)
Global and Chinese market of aircraft MRO software 2022-2028: Research Report on technology, participants, trends, market size and share
RFID让固定资产盘点更快更准
Leetcode skimming: stack and queue 02 (realizing stack with queue)
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
Comprehensive broadcast of global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
Weather forecast applet source code weather wechat applet source code
With the acquisition of Xilinx, AMD is more than "walking on two legs" | Jiazi found
Recently, three articles in the nature sub Journal of protein and its omics knowledge map have solved the core problems of biology
gradle
cookie、session、tooken
excel查找与引用函数
8.8.4-PointersOnC-20220215
How do Lenovo computers connect Bluetooth headsets?
关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
Global and Chinese market of collaborative applications 2022-2028: Research Report on technology, participants, trends, market size and share