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. 比较不错的Nosql文章

    1. NoSQL简单介绍 2. NoSQL初探之人人都爱Redis:(1)Redis简介与简单安装 3. NoSQL初探之人人都爱Redis:(2)Redis API与常用数据类型简介 4. NoSQ ...

  2. kvm虚拟化二: 字符界面管理及 无人值守安装

    1. 安装必要工具yum install / tigervnc //vnc远程桌面客户端 virt-viewer //虚拟机查看器 2.安装虚拟机virt-install / -n 名字 //虚拟机名 ...

  3. 从字节码层面,解析 Java 布尔型的实现原理

    最近在系统回顾学习 Java 虚拟机方面的知识,其中想到一个很有意思的问题:布尔型在虚拟机中到底是什么类型? 要想解答这个问题,我们看 JDK 的源码是无法解决源码的,我们必须深入到 class 文件 ...

  4. docker之搭建私有仓库

    一.私有仓库 1.防止网络原因:下载慢,访问不到的情况,需要在内网搭建一个私有仓库. 二.仓库镜像下载 [root@node03 ~]# docker pull registry 三.创建私有仓库容器 ...

  5. unity2D背景移动补偿从而获得3d错觉效果

    2d平台跳跃游戏当相机移动的时候背景跟随进行微调移动,从而使得玩家获得3d的错觉 using System.Collections;using System.Collections.Generic;u ...

  6. NO--12模拟服务器端请求之node.js

    最近几天项目上线,工作比较忙,没时间更博了,好在今天有点时间并且同事问道我一个问题,正好一块解决 使用 Vue 写项目肯定会遇到一个问题,如何模拟服务端请求数据,那这就需要用到 node.js 了. ...

  7. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  8. PAT甲题题解-1056. Mice and Rice (25)-模拟题

    有n个老鼠,第一行给出n个老鼠的重量,第二行给出他们的顺序.1.每一轮分成若干组,每组m个老鼠,不能整除的多余的作为最后一组.2.每组重量最大的进入下一轮.让你给出每只老鼠最后的排名.很简单,用两个数 ...

  9. FPGA---Basys3(实验内容汇总贴)

    前言 本博文为FPGA---Basys3入门板的实验汇总帖子. 实验指导书 实验源码github地址 实验目录 组合逻辑电路设计 编码器 比较器 全加器 时序逻辑电路设计 D 触发器的实现 同步复位的 ...

  10. Hibernate性能优化之EHCache缓存

    像Hibernate这种ORM框架,相较于JDBC操作,需要有更复杂的机制来实现映射.对象状态管理等,因此在性能和效率上有一定的损耗. 在保证避免映射产生低效的SQL操作外,缓存是提升Hibernat ...