当前位置:网站首页>Leetcode--45. jumping game II (greed)
Leetcode--45. jumping game II (greed)
2022-07-28 17:58:00 【A bone loving cat】
Jumping game Ⅱ(C)
1. Title Description
difficulty : difficult 
2. Topic analysis
The subject is LeetCode55. Jump algorithm An advanced version of , There are several points to note :
- Suppose you can always reach the last position of the array
- Use the least number of jumps
There are two ways to do it :
- Maximum crossing point algorithm
Find the point with the lowest subscript that can reach the last element , That is, it can reach the maximum point of the spanning degree of the last element . Then start at this point , Continue to find the point with the smallest subscript that can reach this point , Know that the subscript of the found point is 0, That is, the first place of the array . The best time complexity is O(n), The worst case scenario is O(n^2) - Greedy Algorithm
Traversing the array from left to right , Calculate the maximum distance that can be reached at each step , And update the maximum distance value . When the subscript of traversal is equal to the maximum distance , It means that you should take a step at this time , Until we're done traversing the array . The time complexity is O(n)
3. C Language implementation
3.1 Maximum crossing point algorithm
The code is as follows :
// Find the maximum crossing point
int findMaxPoint(int* nums, int index){
int i;
for(i = 0; i < index; i++){
if(nums[i]>= index-i)
return i;
}
return i;
}
int jump(int* nums, int numsSize){
int i, index, count;
index = numsSize-1;
count = 0;
if(numsSize == 1) return 0;
while(index != 0){
index = findMaxPoint(nums, index);
count++;
}
return count;
}
The running result is :
3.2 Greedy Algorithm
The code is as follows :
int jump(int* nums, int numsSize){
// ans For the number of times to jump
int ans = 0;
// end So far , The largest point that the previous element can reach
int end = 0;
// maxPos Is the maximum value that is constantly updated
int maxPos = 0;
for (int i = 0; i < numsSize - 1; i++)
{
maxPos = nums[i]+i>maxPos?nums[i]+i:maxPos;
// When the subscript of traversal encounters end when , It means it's time to jump ,ans Add one
if (i == end)
{
end = maxPos;
ans++;
}
}
return ans;
}
The running result is :
边栏推荐
- 这么多开源框架,该用哪个好?
- OpenMV(一)--基础介绍与硬件架构
- 视频号小白起号操作指南
- Understanding of virtual (virtual method) in C and its difference from abstract (abstract method)
- 视频号直播支持商品回放
- Electrotechnics self study notes 1.22
- Prize essay solicitation | the 2022 cloud native programming challenge draft activity is open!
- 数字滤波器(四)--模拟滤波器转化为数字滤波器
- Overflow failure of mobile terminal
- ROS scattered knowledge points and error resolution
猜你喜欢

Encapsulation, inheritance, polymorphism

Point cloud processing -- binary tree

其他电脑连接本地mysql
![[p5.js learning notes] mouse interaction event](/img/d3/b811825c8e4db2c5f840ea6b571de4.png)
[p5.js learning notes] mouse interaction event

IO的操作
![[C language note sharing] character function and string function (recommended Collection)](/img/0a/90cd617e3622d95f9bfc3e945a298a.png)
[C language note sharing] character function and string function (recommended Collection)
![[unity] three pictures let you understand the shadergraph editor](/img/06/cbb9fc84f17fe8682ffd05e02939c3.png)
[unity] three pictures let you understand the shadergraph editor
![[unity] how to play sprite Jiugongge?](/img/cf/112a048c0bb335d058806dd5604d10.png)
[unity] how to play sprite Jiugongge?

abstract、static、final

abstract、static、final
随机推荐
Complete MySQL interview questions (updated in succession)
视频号运营有这个工具就够了
Tips--SCI论文写作中的小技巧
Leetcode systematic question brushing (II) -- greed, backtracking, recursion
@Detailed explanation of requestmapping
OpenMV(二)--IDE安装与固件下载
2.2- data type
Image processing code sorting
Electrotechnics self study notes 1.20
[p5.js] practical exercise - irregular symmetry
视频号如何将公域流量将用户导入私域
abstract、static、final
[p5.js actual combat] my self portrait
[unity] how to play sprite Jiugongge?
How to upload a project to the code cloud using idea
Collection collection
[p5.js learning notes] basic knowledge of code drawing
数字滤波器(五)--设计IIR滤波器
Compilation principle learning notes 2 (Introduction to syntax analysis)
[unity] three pictures let you understand the shadergraph editor