当前位置:网站首页>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;
}
边栏推荐
- 203. Remove linked list elements
- Billions of asset addresses are blacklisted? How to use the tether address freezing function?
- LeetCode_409_最长回文串
- 动态规划——62. 不同路径
- ES6 from getting started to mastering 09: symbol type
- 贪心——55. 跳跃游戏
- leetcode刷题:动态规划08(分割等和子集)
- TypeError: ufunc ‘bitwise_ and‘ not supported for the input types, and the inputs could not be safely
- Implementation of online rental system based on SSM
- leetcode刷题:动态规划09(最后一块石头的重量 II)
猜你喜欢

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

TypeError: ufunc ‘bitwise_ and‘ not supported for the input types, and the inputs could not be safely

Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse

LabVIEW loads and uses custom symbols in tree control projects

什么是Tor?Tor浏览器更新有什么用?

单调栈——42. 接雨水——面大厂必须会的困难题

ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小

What is tor? What is the use of tor browser update?

20220726 at command test of Bluetooth module hc-05 of Huicheng Technology

ES6 从入门到精通 # 07:解构赋值
随机推荐
12月份PMP考试首次采用新考纲,该怎么学?
How to uninstall clean ZABBIX service? (super detailed)
Unity背包系统
动态规划——416. 分割等和子集
Log analysis tool (Splunk)
ES6 from getting started to mastering 09: symbol type
【OPENVX】对象基本使用之vx_pyramid
贪心——122. 买卖股票的最佳时机 II
【力扣】1337.矩阵中战斗力最弱的k行
STM32 RT thread virtual file system mount operation
WordPress简约mkBlog博客主题模板v2.1
动态规划——63. 不同路径 II
LeetCode_409_最长回文串
Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size
Leetcode skimming: dynamic programming 08 (segmentation and subsets)
MySQL Basics (create, manage, add, delete, and modify tables)
某宝模拟登录,减少二次验证的方法
【OPENVX】对象基本使用之vx_distribution
一篇文章掌握Postgresql中对于日期类数据的计算和处理
AI首席架构师12-AICA-百度OCR垂类规模化落地实践