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 ...
随机推荐
- Vue框架之vuex的使用
1.首先需要在你的项目目录下安装vuex 终端命令: 2.在全局组件中导入与声明vuex 3.创建store实例对象 let store = new Vuex.store({ state:{ }, m ...
- Linux命令——logger
参考:How to use logger on Linux Difference between /var/log/messages, /var/log/syslog, and /var/log/ke ...
- 【深度学习】基于Pytorch的ResNet实现
目录 1. ResNet理论 2. pytorch实现 2.1 基础卷积 2.2 模块 2.3 使用ResNet模块进行迁移学习 1. ResNet理论 论文:https://arxiv.org/pd ...
- 2019-ACM-ICPC-南京区网络赛-E. K Sum-杜教筛+欧拉定理
2019-ACM-ICPC-南京区网络赛-E. K Sum-杜教筛+欧拉定理 [Problem Description] 令\(f_n(k)=\sum_{l_1=1}^n\sum_{l_2=1}^n\ ...
- Socket实现client和server端通信(Java)(转)
转自: https://blog.csdn.net/yayun0516/article/details/50819147 https://www.jianshu.com/p/2d4f223f1462 ...
- Python读excel——xlrd
Python读excel——xlrd Python读取Excel表格,相比xlwt来说,xlrd提供的接口比较多,但过程也有几个比较麻烦的问题,比如读取日期.读合并单元格内容.下面先看看基本的操作: ...
- k8s的组件
1.Master组件 1.API Server K8S对外的唯一接口,提供HTTP/HTTPS RESTful API,即kubernetes API.所有的请求都需要经过这个接口进行通信.主要负责接 ...
- 聊聊MVCC多版本并发控制
一.介绍 MVCC只在RR和RC 2个隔离级别下才能工作.MySQL的大多数事务存储引擎实现的都不是简单的行级锁机制.基于提升并发性能的考虑,它们一般都同时实现了MVCC. 通俗的来讲,MVCC是行级 ...
- tomcat配置虛擬路徑
1.server.xml设置 打开Tomcat安装目录,在server.xml中<Host>标签中,增加<Context docBase="硬盘目录" path= ...
- BZOJ2956: 模积和——整除分块
题意 求 $\sum_{i=1}^n \sum_{j=1}^m (n \ mod \ i)*(m \ mod \ j)$($i \neq j$),$n,m \leq 10^9$答案对 $1994041 ...