当前位置:网站首页>贪心——55. 跳跃游戏
贪心——55. 跳跃游戏
2022-07-28 03:21:00 【向着百万年薪努力的小赵】
1 题目描述
给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个下标。
2 题目示例
示例 1:
输入:nums = [2,3,1,1,4]
输出:true
解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
示例 2:
输入:nums = [3,2,1,0,4]
输出:false
解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/jump-game
3 题目提示
1 <= nums.length <= 3 * 104
0 <= nums[i] <= 105
4 思路
设想一下,对于数组中的任意一个位置y,我们如何判断它是否可以到达y根据题目的描述,只要存在一个位置x,它本身可以到达,并且它跳跃的最大长度为x + nums[x],这个值大于等于y,即x+ nums[x] ≥y,那么位置y 也可以到达。
换句话说,对于每一个可以到达的位置x,它使得x+1, x+2,… ,+ nums[x]这些连续的位置都可以到达。
这样一来,我们依次遍历数组中的每一个位置,并实时维护最远可以到达的位置。对于当前遍历到的位置x,如果它在最远可以到达的位置的范围内,那么我们就可以从起点通过若干次跳跃到达该位置,因此我们可以用x+ nums[x]更新最远可以到达的位置。
在遍历的过程中,如果最远可以到达的位置大于等于数组中的最后一个位置,那就说明最后一个位置可达,我们就可以直接返回True作为答案。反之,如果在遍历结束后,最后一个位置仍然不可达,我们就返回False作为答案。
以题目中的示例一
[2,3,1,1,4]
为例:
- 我们—开始在位置0,可以跳跃的最大长度为2,因此最远可以到达的位置被更新为2;
- 我们遍历到位置1,由于1≤2,因此位置1可达。我们用1加上它可以跳跃的最大长度3,将最远可以到达的位置更新为4。由于4大于等于最后一个位置4,因此我们直接返回True 。
我们再来看看题目中的示例二
[3, 2, 1, 0, 4]
- 我们—开始在位置0,可以跳跃的最大长度为3,因此最远可以到达的位置被更新为3;
- 我们遍历到位置1,由于1≤3,因此位置1可达,加上它可以跳跃的最大长度⒉得到3,没有超过最远可以到达的位置;
- 位置2、位置3同理,最远可以到达的位置不会被更新
- 我们遍历到位置4,由于4 > 3,因此位置4不可达,我们也就不考虑它可以跳跃的最大长度了。
在遍历完成之后,位置4仍然不可达,因此我们返回False 。
复杂度分析
- 时间复杂度:O(n),其中n为数组的大小。只需要访问nums数组—遍,共n个位置。
- 空间复杂度:O(1),不需要额外的空间开销。
5 我的答案
public class Solution {
public boolean canJump(int[] nums) {
int n = nums.length;
int rightmost = 0;
for (int i = 0; i < n; ++i) {
if (i <= rightmost) {
rightmost = Math.max(rightmost, i + nums[i]);
if (rightmost >= n - 1) {
return true;
}
}
}
return false;
}
}
边栏推荐
- Redis implements distributed locks
- Win11输入法的选字框不见了怎么办?
- 2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改, 可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同), 并使得所有数之和为X的倍数。
- Volvo: what on earth does the deep-rooted "sense of security" rely on?
- 自定义注解的使用
- SQL Server备份数据库的方法
- [uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 2/100
- 每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
- Hotel VR panoramic display shooting provides more opportunities for cooperation and negotiation
- How to make the Internet access the intranet IP (used by esp8266 web pages)
猜你喜欢

STM32 RT thread virtual file system mount operation

Redis basic operation

Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size

20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration

When a dialog box pops up, the following form is not available

Summary of redis classic interview questions
![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](/img/24/fbf63272f83b932e0ee953f8d4db75.png)
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

How to arrange PCB screen printing? Please check this manual!

收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南

响应式高端网站模板源码图库素材资源下载平台源码
随机推荐
Billions of asset addresses are blacklisted? How to use the tether address freezing function?
Volvo: what on earth does the deep-rooted "sense of security" rely on?
203. Remove linked list elements
[R language] environment specifies to delete RM function
某宝模拟登录,减少二次验证的方法
D2dengine edible tutorial (4) -- draw text
How to reinstall win11 system with one click
一键重装win7系统详细教程
数字经济已成为最大看点
Softek Barcode Reader 9.1.5
LabVIEW加载和使用树型控件项目中的定制符号
Robot development -- lead screw and guide rail
Four methods of closing forms in C #
每周推荐短视频:如何正确理解“精益”这个词?
Leetcode 208. implement trie (prefix tree) (2022.07.27)
[download file] uniapp develops small programs, downloads files and saves them locally
自定义注解的使用
同时导出多个excel,并且一个excel中包含多个sheet
Redis memory recycling
20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration