当前位置:网站首页>Jumping game II in question 45 of C language power deduction. Ergodic jump
Jumping game II in question 45 of C language power deduction. Ergodic jump
2022-07-28 03:42:00 【Take care of two dogs and never let them go bad】
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
- If one acts as Take off point The distance that a grid can jump is 3, Then it means that 3 Each grid can be used as Take off point . Can be for everyone who can do Take off point All the squares try to jump once , hold Can jump to the farthest distance Constantly updated .
- If we start from this Take off point The take-off is called the first 1 Time jumping , So from the back 3 A grid take-off all It can be called the first 2 Time jumping .
- therefore , When a jumping At the end , Start with the next grid , Up to now Can jump to the farthest distance , all It's the next time jumping Of Take off point .
31. For every time jumping use for Loop to simulate .
32. After one jump , Update next time Take off point The scope of the .
33. Jump in a new range , to update Can jump to the farthest distance .
Record jumping frequency , If you jump to the end , And you get the result .
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int jump(int* nums, int numsSize)
{
int end = 0, farthest = 0;
int jumps = 0;
int i = 0;
for (i = 0; i < numsSize - 1; i++)
{
// Judge the current farthest reachable position
farthest = fmax(nums[i] + i, farthest);
// Update the boundary and increase the number of jumps 1.
if (end == i)
{
jumps++;
end = farthest;
}
}
// Return the number of jumps
return jumps;
}
边栏推荐
- deepstream 检测结果截图
- 面试必备杀技:SQL查询专项训练!
- 【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
- [openvx] VX for basic use of objects_ distribution
- CH340 RTS DTR引脚编程驱动OLED
- In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial
- Qt:qmessagebox message box, custom signal and slot
- 接口自动化测试,完整入门篇
- LabVIEW loads and uses custom symbols in tree control projects
- Volvo: what on earth does the deep-rooted "sense of security" rely on?
猜你喜欢

Server memory failure prediction can actually do this!

"Xiaodeng" network equipment monitoring in operation and maintenance

An article grasps the calculation and processing of date data in PostgreSQL

Vertical align align the elements in the row are vertically centered

Super nice PHP program source code of nteam official website

How to solve MySQL deep paging problem

Daily practice ----- realize the lottery function of two-color ball. Rules: Six non repeating numbers are randomly selected from 36 red balls, and one from 15 basketball is randomly selected to form a

LabVIEW加载和使用树型控件项目中的定制符号

After 95, Alibaba P7 published the payroll: it's really heartbreaking

ASEMI整流桥GBPC3510在直流有刷电机中的妙用
随机推荐
基于SSM实现在线租房系统
Light year admin background management system template
[wrong question]
ES6 从入门到精通 # 07:解构赋值
MangoPapa 的实用小脚本(目录篇)
Digital economy has become the biggest attraction
Xctf attack and defense world web master advanced area unserialize3
递归和非递归分别实现求第n个斐波那契数
收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南
什么是Tor?Tor浏览器更新有什么用?
高等数学(第七版)同济大学 习题3-5 个人解答
"Xiaodeng" network equipment monitoring in operation and maintenance
[openvx] VX for basic use of objects_ convolution
动态规划——474. 一和零
数据挖掘-02
高等数学(第七版)同济大学 习题3-4 个人解答(后8题)
[wrong question]mocha and railgun
Log analysis tool (Splunk)
【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial