【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 ...
随机推荐
- Android开发——Android进程保活招式大全
)前台进程(Foreground process),即用户当前操作所必需的进程,通常数量不多.举例如下: //拥有用户正在交互的 Activity(已调用 onResume()) //拥有某个 Ser ...
- how2j 的shiro教程初探
教程案例里的mysql连接器只支持mysql,不支持mariadb,如果用的不是mysql,创建连接时会报错.
- mybatis 异常 too many connections 解决方案 mysql
参考: https://blog.csdn.net/u011628250/article/details/54017481 https://www.cnblogs.com/baby123/p/5710 ...
- 分布式Redis缓存串讲(一)
互联网应用的基石 现在流量稍微大些的网站,都会采取Redis.基于Redis的内存缓存特性,可以大幅度降低数据库的访问量,大大提升了网站的并发能力,充当数据库的削量先锋.既然Redis这么重要,我们从 ...
- Python基础知识(Basic knowledge)
Python基础知识(Basic knowledge) 1.认识Python&基础环境搭建 2.Python基础(上) 3.Python基础(中) 4.Python基础(下) 5.Python ...
- java向上转型和向下转型
转型是在继承的基础上而言的,继承是面向对象语言中,代码复用的一种机制,通过继承,子类可以复用父类的功能,如果父类不能满足当前子类的需求,则子类可以重写父类中的方法来加以扩展. 向上转型:子类引用的对象 ...
- ElasticSearch 2 (22) - 语言处理系列之标记规范化
ElasticSearch 2 (22) - 语言处理系列之标记规范化 摘要 将文本拆解成标记只是工作的一半.为了使这些标记更容易被搜索到,它们需要经过一个规范化的处理过程,以移除相同单词间不重要的差 ...
- Photoshop的混合模式
1.亮度是一种颜色的相对亮度,饱和度是指一种颜色的纯度(颜色中包含多少灰) 2.混合模式 下层图片的颜色像素称为"基本颜色":选定的称为"混合"颜色,对于大部分 ...
- Linux搭建好apache后,只有本地能访问,局域或外网不能访问
由于防火墙的访问控制导致本地端口不能被访问. 解决方法: 1,直接关闭防火墙 systemctl stop firewalld.service #停止防火墙服务 systemctl disable ...
- php artisan 命令列表
php artisan 命令列表 命令获取 上面的翻译内容 命令 说明 备注 php artisan make:resource ? 创建api返回格式化资源 >=5.4版本可用 php ar ...