【leetcode】 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4]
, return true
.
A = [3,2,1,0,4]
, return false
.
题意:给定一组数组,每个元素代表在此位置能够跳跃的最大距离,判断是否能够跳到最后一个下标。
有三种思路:
- 正向从 0 出发,一层一层往右跳,看最后能不能超过最右下标,能超过,说明能到达,否则不能到达。
- 逆向出发,一层一层递减,看能不能到达0.
- 可以用动规,设状态为 f[i],表示从第 0 层出发,走到 A[i] 时剩余的最大步数,则状态转移方程为:f [i] = max(f [i − 1], A[i − 1]) − 1, i > 0
#include <iostream>
#include <vector> using namespace std; class Solution {
public:
bool canJump(int A[], int n) {
int reach = ;//the right most position can reach
for(int i = ; i < reach && reach < n; i++)
reach = max(reach, i + + A[i]);
return reach >= n;
} bool canJump1(int A[], int n) {
if(n == ) return true;
int left_most = n - ;//the left most position can reach
for(int i = n - ; i >= ; --i)
if(i + A[i] >= left_most)
left_most = i;
return left_most == ;
} bool canJumpDp(int A[],int n){
//f [i] = max(f [i − 1], A[i − 1]) − 1, i > 0
vector<int> f(n, );//hold the state
f[] = ;
for(int i = ; i < n; ++i){
f[i] = max(f[ i - ], A[i - ]) - ;
if(f[i] < )
return false;
}
return f[n - ] >= ;
}
}; int main()
{
Solution s;
int A1[] = {,,,,};
int A2[] = {,,,,};
cout << s.canJumpDp(A1, ) << endl;
cout << s.canJumpDp(A2, ) << endl;
return ;
}
【leetcode】 Jump Game的更多相关文章
- 【LeetCode】Jump Game (一维动态规划 + 线性扫描)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode】Jump Game I & II (hard)
Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...
- 【Leetcode】Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode】Jump Game I, II 跳跃游戏一和二
题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...
- 【LeetCode】Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- EZ 2018 04 06 NOIP2018 模拟赛(七)
我是链接 这次是真的惨,码了将近2hours的可持久化线段树炸掉了! 而且本地拍了一万年也没发现哪里炸了. T1 压位的入门题,话说这道题能拿个99分就可以了(100分要FFT) 对于暴力,就是暴力找 ...
- 解决Git在添加ignore文件之前就提交了项目无法再过滤问题
由于未添加ignore文件造成提交的项目很大(包含生成的二进制文件).所以我们可以将编译生成的文件进行过滤,避免添加到版本库中了. 首先为避免冲突需要先同步下远程仓库 $ git pull 在本地项目 ...
- Solr 后台查询实例 (工作备查)
有时间再进行整理package xxx.service.impl; import java.util.HashMap; import java.util.Map; import java.util.M ...
- identityServer4 中的概念(Scope,claim)
在IdentityServer中好多地方出现这几个词,这单词的解释也有好多大神解释过: chaim: ASP.NET Core 之 Identity 入门(一),这个是asp.net identity ...
- Linux删除多余内核
查看已安装内核 sudo dpkg --get-selections |grep linux-image 查看当前内核 uname -r 卸载内核 sudo apt-get remove 内核名称 配 ...
- 【Alpha】第八次Scrum meeting
今日任务一览: 姓名 今日完成任务 所耗时间 刘乾 学习js并学会使用js读写xml文件.学习python读取xml的方式... 然后上午满课,下午从1点到10点当计组助教去沙河教了一下午+一晚上,所 ...
- NPM和Yarn添加淘宝镜像
NPM设置淘宝镜像 1.查询当前配置的镜像 npm get registry > https://registry.npmjs.org/ 设置成淘宝镜像 npm config set regis ...
- Visual Studio 2013安装及简单的单元测试
Visual Studio 2013安装部分首先到观望下载了英文版和简体中文的语言包.但是更新语言包时总是失败,安装vs需要等待的时间也很大.后来重新装了一遍系统(这个卸载也很麻烦),从其他网站下了中 ...
- 刚做了个实验,验证++和!和printf的先后次序
- Ubuntu16解锁root
administrator@rgqancy:~$ sudo passwd -u root [sudo] administrator 的密码: 对不起,请重试. [sudo] administrator ...