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 ...
随机推荐
- ubuntu16搭建LAMP环境
准备工作: 安装ubuntu16虚拟机,可以正常访问网络 更新为国内源(下载快一些) 1.安装apache sudo apt-get install apache2 然后打开我们的浏览器,访问一下 1 ...
- Go 性能分析之案例一
思考 相信大家在实际的项目开发中会遇到这么一个事,有的程序员写的代码不仅bug少,而且性能高:而有的程序员写的代码能否流畅的跑起来,都是一个很大问题.而我们今天要讨论的就是一个关于性能优化的案例分析. ...
- linux网络编程之posix消息队列
在前面已经学习了System v相关的IPC,今天起学习posix相关的IPC,关于这两者的内容区别,简单回顾一下: 而今天先学习posix的消息队列,下面开始: 接下来则编写程序来创建一个posix ...
- 本地jar通过maven命令导到本地仓库里
mvn install:install-file -Dfile=D:\repo\mybtais-generator-1.0.0.jar -DgroupId=mybatis.plugins -Darti ...
- markdown 显示图片的三种方式
插入网络图片 插入本地图片 base64 图片(data:image/png;base64,iVBORw0KG........) ps:base64编码的图片可以通过站长工具编码 https://to ...
- Centos 安装JDK(最最最最最方便的方法)
1.下载rpm安装文件,链接:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2 ...
- php MySQL 数据类型
MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准S ...
- java调出cmd窗口长ping某个ip
package lct.conference.test; import java.io.IOException; public class Test { public static void main ...
- 对拍——>bat
为了凸显对拍滴重要性.就拿来当置顶啦! ——本来是那样想的 ---------------------------------------------------------------------- ...
- Centos 如何扩充/增加磁盘
1:使用背景 废话不多说,磁盘空间不足,增加磁盘,然后扩充现有不足空间磁盘. 本次以Vmware进行测验. 2:我们本次要增加的就是这个 3:我们先添加一个磁盘,20G,添加过程不在赘述 4:添加完成 ...