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的更多相关文章

  1. 【LeetCode】Jump Game (一维动态规划 + 线性扫描)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. 【leetcode】Jump Game I & II (hard)

    Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...

  3. 【Leetcode】Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. 【leetcode】Jump Game I, II 跳跃游戏一和二

    题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...

  5. 【LeetCode】Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. MySQL优化:explain using temporary

    什么时候会使用临时表:group/order没设计好的时候 1.order没用索引 2.order用了索引, 但不是和where相同的索引 3.order用了两个索引, 但不是联合索引 4.order ...

  2. VMware桥接模式连接局域网

    今天尝试虚拟机直连家里的局域网,用于方便另外一台主机使用家里的虚拟机. 本次连接方式是通过桥接方式,但由于'桥接到'选项默认自动,导致无法连通,最终以下步骤完成配置: 第一步:确认本地网关地址 第二步 ...

  3. 3.RapidIO串行物理层的包传输过程

    转自https://www.cnblogs.com/liujinggang/p/10005431.html 一.引言 前几篇文章已经谈到RapidIO的协议,串行物理层与控制符号. RapidIO协议 ...

  4. jQuery .attr() vs. .prop()

    Property vs. Attribute 在开始正式比较prop()和attr()两个jQuery方法之前,我们有必要先弄清一下Property和Attribute两个单词的意思.在中文里面,它们 ...

  5. 创建并使用maven archetype的随笔

    maven骨架archetype的意义在于一些项目的基础项:如引入的maven组件,例如eureka,ribben等,不希望每次新建项目都重复做一遍,还有例如公司规范的log格式,单元测试工具等,在新 ...

  6. js获取浏览器对象的信息

    js中有一个对象叫 navigator,navigator 对象包含有关浏览器的信息.所有的浏览器都支持该对象. 其中经常用到的是 navigator.userAgent 属性,通常,它是在 navi ...

  7. Ing_制作在线QQ

    制作在线QQ的具体步骤 1.首先登录到http://is.qq.com/webpresence/code.shtml 网站2.选择风格3.填写相关数据4.生成网页代码5.复制代码到“写字板”,另存文件 ...

  8. 【原】python3.7 无法pip安装提示ssl错误解决方案

    问题 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not av ...

  9. Salesforce Apex学习 : 利用Schema命名空间中的DescribeSObjectResult类型来获取sObject对象的基本信息

    DescribeSObjectResult 对象的取得: //使用getDescribe方法和sObject token Schema.DescribeSObjectResult mySObjDesc ...

  10. 利用HOG+SVM实现行人检测

    利用HOG+SVM实现行人检测 很久以前做的行人检测,现在稍加温习,上传记录一下. 首先解析视频,提取视频的每一帧形成图片存到磁盘.代码如下 import os import cv2 videos_s ...