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 ...
随机推荐
- 运行 jcontrol 报 libXext.so.6: cannot open shared object file 错误
需要安装额外库: yum install libXext.x86_64 yum install libXrender.x86_64 yum install libXtst.x86_64
- 小程序数据绑定和setData
我们wxml没有直接调用数据的能力,我们的逻辑是通过js调用数据,再由js传递给wxml才能够显示出来.那么怎么由js传递给wxml? 首先我的js里面有这样一段代码 process: funct ...
- k8s的包管理
1.Helm的概念和架构 每个成功的软件平台都有一个优秀的打包系统,比如 Debian.Ubuntu 的 apt,Redhat.Centos 的 yum.而 Helm 则是 Kubernetes 上的 ...
- P1880 [NOI1995]石子合并[环形DP]
题目来源:洛谷 题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将 ...
- PL/SQL嵌入SQL语句
一.PL/SQL块中只能直接嵌入SELECT.DML(INSERT,UPDATE,DELETE)以及事务控制语句(COMMIT,ROLLBACK,SAVEPOINT), 而不能直接嵌入DDL语句(CR ...
- vue2 运动及相关函数
- 简介WEB应用服务器TONCAT
1.HTTP与Tomcat简介1.1 什么是Http协议HTTP,超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的 一种网络协议.所有的WWW文件都必须 ...
- 2018 Arab Collegiate Programming Contest (ACPC 2018) G. Greatest Chicken Dish (线段树+GCD)
题目链接:https://codeforces.com/gym/101991/problem/G 题意:给出 n 个数,q 次询问区间[ li,ri ]之间有多少个 GCD = di 的连续子区间. ...
- Java处理小数点后几位
//方式一: //四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); , BigDecimal.ROUND_HALF_UP). ...
- k8s aliyun mirros
安装kubernetes的时候,需要安装kubelet, kubeadm等包,但k8s官网给的yum源是packages.cloud.google.com,国内访问不了,此时我们可以使用阿里云的yum ...