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安装java环境和maven
安装OpenJDK软件包: apt-get install openjdk-8-jdk 查看版本信息java -version 则代表安装成功 安装maven可以使用自己本机下载好的mavan使用Xf ...
- 2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组
2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组 [Problem Description] 给你一个\([1,n]\)的排列,查询\([l,r]\)区间内有多少对 ...
- bloomberg bulkfile【一】 文件的分类
文章导航 bloomberg bulkfile [一] 文件的分类 bloomberg bulkfile [二] 文件解析 bloomberg bulkfile [三] 在oracle的存储 订 ...
- 运维CMDB建设思路
在我们日常的运维工作中,面对着大量的基础设施和软件服务,该如何管理?这个管理的原则又是什么?粒度该如何控制?我们是否可以建立一个统一的标准模型来管理以上对象?管理过程中,如何降低人力成本?资源对象的生 ...
- 回调函数(callback)
回调函数(callback) A "callback" is any function that is called by another function which takes ...
- DML子句returing into用法举例
一.概述: ORACLE的DML语句中可以指定RETURNING语句.使用起来也很简单,和SELECT INTO语句没有多大区别.RETURNING语句的使用在很多情况下可以简化PL/SQL编程. I ...
- vue1 监听数据变化
- JavaScript常用的方法
indexOf() 功能:indexOf() 方法返回调用 String 对象中第一次出现的指定值的索引. 语法:indexOf(searchValue, fromIndex) searchValue ...
- 32 | 为什么还有kill不掉的语句?
在 MySQL 中有两个 kill 命令:一个是 kill query + 线程 id,表示终止这个线程中正在执行的语句:一个是 kill connection + 线程 id,这里 connecti ...
- Qt进程间通信
Qt 提供了四种进程间通信的方式: 使用共享内存(shared memory)交互:这是 Qt 提供的一种各个平台均有支持的进程间交互的方式. TCP/IP:其基本思想就是将同一机器上面的两个进程一个 ...