当前位置:网站首页>Greed 45. Jumping game II
Greed 45. Jumping game II
2022-07-28 03:44:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
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 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/jump-game-ii
2 Title Example
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
3 Topic tips
1 <= nums.length <= 104
0 <= nums[i] <= 1000
4 Ideas
This problem is a typical greedy algorithm , The global optimal solution is obtained through the local optimal solution .
Method 1 :
Our goal is to reach the last position of the array , So we can consider the last — The position before the jump , This position can reach the last position by jumping .
If there are multiple positions, you can reach the last position by jumping , So how should we choose ? intuitively , We can 「 greedy 」 Choose the location farthest from the last location , That is, the position corresponding to the smallest subscript . therefore , We can traverse the array from left to right , Select the first location that meets the requirements .
Find the last — After the position before the jump , We continue to greedily look for the position before the penultimate jump , And so on , Until you find the beginning of the array .
Method 1 is intuitive , But the time complexity is high , Is there any way to reduce the time complexity ?
If we 「 greedy 」 Forward lookup , Find the farthest reachable position every time , You can get the least number of jumps in linear time .
for example , For arrays [2,3,1,2,4,2,3], The initial position is the subscript 0, From the subscript О set out , The longest reachable subscript 2. Subscript 0 In an accessible location , Subscript 1 The value of is 3, From the subscript 1 You can go further , So the first step is to reach the subscript 1.
From the subscript 1 set out , The longest reachable subscript 4. Subscript 1 In an accessible location , Subscript 4 The value of is 4, From the subscript 4 You can go further , So the second step reaches the subscript 4.
In the concrete realization , We maintain the maximum subscript position currently reachable , Mark as boundary . We traverse the array from left to right , When we reach the border , Update the boundary and increase the number of jumps 1.
When you go through groups , We don't access the last element , This is because before accessing the last element , Our boundary must be greater than or equal to the last position , Otherwise you can't jump to the last position . If you access the last element , When the boundary is exactly the last position , We'll add one 「 Number of unnecessary jumps 」, So we don't have to access the last element .
5 My answer
class Solution {
public int jump(int[] nums) {
int position = nums.length - 1;
int steps = 0;
while (position > 0) {
for (int i = 0; i < position; i++) {
if (i + nums[i] >= position) {
position = i;
steps++;
break;
}
}
}
return steps;
}
}
class Solution {
public int jump(int[] nums) {
int length = nums.length;
int end = 0;
int maxPosition = 0;
int steps = 0;
for (int i = 0; i < length - 1; i++) {
maxPosition = Math.max(maxPosition, i + nums[i]);
if (i == end) {
end = maxPosition;
steps++;
}
}
return steps;
}
}
边栏推荐
- input 上传文件并回显 FileReader并限制选择文件时的类型
- Vertical align align the elements in the row are vertically centered
- Jumping game II in question 45 of C language power deduction. Ergodic jump
- What is tor? What is the use of tor browser update?
- Practical scripts of mangopapa (contents)
- 我的创作纪念日
- Swift中的协议
- Leetcode skimming: dynamic programming 08 (segmentation and subsets)
- 数据挖掘-02
- 2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb
猜你喜欢

构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!

Implementation of online rental system based on SSM

Weekly recommended short video: how to correctly understand the word "lean"?

Billions of asset addresses are blacklisted? How to use the tether address freezing function?

一篇文章掌握Postgresql中对于日期类数据的计算和处理

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

Swift中的协议

Tensorboard usage record

一文读懂Plato Farm的ePLATO,以及其高溢价缘由

Lightpicture - exquisite drawing bed system
随机推荐
LeetCode 0141. 环形链表 - 三种方法解决
A treasure simulates login and reduces the method of secondary verification
面试必备杀技:SQL查询专项训练!
Container related concepts
贪心——55. 跳跃游戏
Weekly recommended short video: how to correctly understand the word "lean"?
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (the last 8 questions)
Collection | 0 basic open source data visualization platform flyfish large screen development guide
input 上传文件并回显 FileReader并限制选择文件时的类型
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb
Greedy - 53. Maximum subarray sum
高等数学(第七版)同济大学 习题3-4 个人解答(后8题)
Recursion and non recursion are used to calculate the nth Fibonacci number respectively
In December, the PMP Exam adopted the new syllabus for the first time. How to learn?
BRD,MRD,PRD的区别
Data mining-02
【OPENVX】对象基本使用之vx_matrix
服务器内存故障预测居然可以这样做!
【论文笔记】基于深度学习的移动机器人自主导航实验平台
Server memory failure prediction can actually do this!