leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game
第一种方法:
只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break
class Solution {
public:
bool canJump(vector<int>& nums) {
int length = nums.size();
if(length <= )
return false;
vector<bool> dp(length,false);
dp[] = true;
for(int i = ;i < length;i++){
for(int j = i - ;j >= ;j--){
if(dp[j] == true && i - j <= nums[j]){
dp[i] = true;
break;
}
}
}
return dp[length-];
}
};
第二种方法:
第一种方法时间复杂度高且需要O(n)的空间复杂度。这题用贪心在O(n)的时间复杂度,O(1)的空间复杂度就可以解决。
用一个reach变量记录当前位置能达到的最远的位置索引,每次更新,如果reach比当前的索引小伙子reach已经能到达最后一个位置就可以break掉。
注意:reach及其比较的对象都是数组的索引,即n-1。
https://www.cnblogs.com/grandyang/p/4371526.html
class Solution {
public:
bool canJump(vector<int>& nums) {
if(nums.empty())
return false;
int reach = ;
for(int i = ;i < nums.size();i++){
if(reach < i || reach >= nums.size() - )
break;
reach = max(reach,i + nums[i]);
}
return reach >= nums.size() - ;
}
};
45. Jump Game II
https://www.cnblogs.com/grandyang/p/4373533.html
pre记录的是前一次能达到的最远位置的索引,cur是当前能达到的最远位置的索引。
你每一次的更新其实都是在前一次最远距离的范围内更新下一次的最远距离。
这个i是当前遍历的位置。
class Solution {
public:
int jump(vector<int>& nums) {
int cur = ,res = ,i = ;
int pre;
while(cur < nums.size() - ){
res++;
pre = cur;
for(;i <= pre;i++)
cur = max(cur,i + nums[i]);
}
return res;
}
};
leetcode 55. Jump Game、45. Jump Game II(贪心)的更多相关文章
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- 力扣Leetcode 45. 跳跃游戏 II - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
- LeetCode 55. 跳跃游戏(Jump Game)
题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...
- 贪心——55. 跳跃游戏 && 45.跳跃游戏II
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Linux的进程管理基本指令
在Linux操作系统中,进程是指一个程序的运行实例,它需要存储器来存储程序本身及其操作数据.内核负责创建和跟踪进程.当程序运行时,内核首先准备好一些内存,将可执行代码从文件系统加载到内存里,然后开始运 ...
- 十年种树----小白的起点save
大家好,给大家介绍一下我自己.各平台通用ID:琴鬼白羊,男,一个24岁学习采矿工程的在读研究僧,一个24岁还在想学习计算机的小白. 非洲经济学家Dambisa Moyo在他的<dead aid& ...
- jstack、jmc、jhat工具使用详解
jstack: 在上一次[https://www.cnblogs.com/webor2006/p/10669472.html]jcmd中也可以获取线程的堆栈信息,回顾一下: 其实在JDK中还有另一个专 ...
- flutter,flutter版本version/channel切换问题
flutter go,官方的指南版本如下: 如何设置版本和channel,尝试 flutter help,发现原来flutter version不单是可以查所有版本(--version查当前版本)还可 ...
- archlinux 使用 postgresql
一.安装与初始化 1.初始化数据目录 默认安装后已创建 postgres 系统用户 切换到 postgres 用户 $ sudo -iu postgres # Or su - postgres for ...
- Java原子类--AtomicLong
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3514593.html AtomicLong介绍和函数列表 AtomicLong是作用是对长整形进行原子操 ...
- 51nod 1459 & 1212
1459 双限制最短路 #include <stdio.h> #include <iostream> #include <vector> #include < ...
- linux查看系统启动时间
1.uptime命令 felix@felix-computer:~$ uptime :: up :, user, load average: 0.89, 0.74, 1.00 felix@felix- ...
- NetworkX系列教程(10)-算法之四:拓扑排序与最大流问题
小书匠Graph图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定先把图 ...
- JDK8中好用的日期处理-LocalDate类-LocalTime-LocalDateTIme,mysql解决时区相差13小时的问题,日期格式器DateTimeFormatter
set global time_zone='+08:00'; set time_zone = '+08:00'; show variables like '%time_zone:'