当前位置:网站首页>55. Jumping game
55. Jumping game
2022-07-24 07:55:00 【Little cloth on the street】
55. Jumping game
Topic link
https://leetcode.cn/problems/jump-game/
One 、 Title Description
Given an array of nonnegative integers nums , You're in the beginning of the array The first subscript .
Each element in the array represents the maximum length you can jump at that location .
Judge whether you can reach the last subscript .
- Example 1:
Input :nums = [2,3,1,1,4]
Output :true
explain : You can jump first 1 Step , From the subscript 0 Reach subscript 1, And then from the subscript 1 jump 3 Step to the last subscript .
- Example 2:
Input :nums = [3,2,1,0,4]
Output :false
explain : No matter what , Always arrive, subscript 3 The location of . But the maximum jump length of the subscript is 0 , So it's never possible to reach the last subscript .
- Tips :
1 <= nums.length <= 3 * 104
0 <= nums[i] <= 105
Two 、 Ideas
When I first saw this question, I might think : If the current location element is 3, What on earth am I doing , Or two steps , Or three steps , How many steps is the best ?
In fact, it doesn't matter how many steps you take , The key is the coverage that can jump !
You don't have to know exactly how many steps to jump at a time , Take the maximum number of jump steps each time , This is the coverage that can jump .
In this range , Never mind how you jump , You can jump over anyway .
Then this question turns into whether the jump coverage can cover the end point !
Take the maximum number of jump steps per move ( Get maximum coverage ), Every unit moved , Update the maximum coverage .
Greedy algorithm local optimal solution : Take the maximum number of jump steps each time ( Take the maximum coverage ), The global optimal solution : Finally, the overall maximum coverage , See if we can reach the end .
The local optimum leads to the global optimum , No counterexample , Try greed !
class Solution {
public:
bool canJump(vector<int>& nums) {
int cover = 0;
if (nums.size() == 1) return true; // There's only one element , It's about being able to get there
for (int i = 0; i <= cover; i++) {
// Notice that this is less than or equal to cover
cover = max(i + nums[i], cover);
if (cover >= nums.size() - 1) return true; // It indicates that the end point can be covered
}
return false;
}
};
- Time complexity :O(n)
- Spatial complexity :O(1)
i Each move can only be made in cover Moving within the limits of , Every time you move an element ,cover Get the value of this element ( New coverage ) A supplement to , Give Way i Keep moving .
and cover Take only max( The range after the value of this element is supplemented , cover Scope of itself ).
If cover Greater than or equal to the end subscript , direct return true That's all right. .
边栏推荐
- Introduction to C language III Array 4. Operators
- Thesis reading: geotransformer
- Have you seen the interview questions of VR major? Trust me, it's absolutely useful
- Selenium basic knowledge debugging method
- Do you want to have a robot that can make cartoon avatars in three steps?
- MS SQL Server 2019 learning
- Workspace creation
- 简易网闸-内网服务器安全获取外网数据
- Opencv project practice - credit card recognition
- Facing Tencent (actual combat) - Test Development - detailed explanation of interns (face experience)
猜你喜欢

Digital twin demonstration project -- Talking about simple pendulum (3) solid model exploration
![Telecom Customer Churn Prediction challenge baseline [AI competition]](/img/ad/2cd108eaffce3a618525727d9b5034.png)
Telecom Customer Churn Prediction challenge baseline [AI competition]

Amber tutorial A17 learning - concept

Why is knowledge base important? This is the best answer I've ever heard

EZDML逆向工程导入数据库分析实操教程

Sense dimension design responsive layout

Debug No1 summarizes common solutions to bugs

HCIP第七天

【sklearn】tree.DecisionTreeClassifier

Advanced part of C language IV. detailed explanation of user-defined types
随机推荐
【Pytorch】nn.Module
Image feature SIFT (scale invariant feature transform)
MySQL update uses case when to update the value of another field according to the value of one field
What is the NFT concept.. Fully understand NFT market, technology and cases
Anaconda cannot shut down the method of forced shutdown
多种优化方法打印100~200之间的素数
Hcip day 8 notes
Collection of binary tree topics
Intelligent robots and intelligent systems (Professor Zheng Zheng of Dalian University of Technology) -- 1. robots and mobile robots
Generative model and discriminant model
【Pytorch】Dataset_ DataLoader
基于VSCode聊聊编译器那些事儿
hcip第十三天笔记
*Project recurrence * project implementation of thesis based on contextbasedemotionrecognitionusingematicdataset
Selenium basic knowledge multi window processing
Hcip day 7
Solve the problem that Anaconda navigator cannot be opened
Debug No4 use renderdoc to troubleshoot bugs
Starting from scratch C language intensive Part 3: Functions
Sense dimension design responsive layout