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.

问题:给定一个数字,假设你站在第一个元素,每个元素表示你可以跳得最大距离。求你是否可以跳到最后一个元素。

思路一,假设已知 A[i+1...n] 是否可以跳都终点(最后一个元素),则 A[i] 是否可以跳到终点只需要检查 A[i+k] 是否可以跳到终点即可。

这实际是一个 DP 思路。对于每个 A[i] 都可能检查大概 (n-i) 次,时间复杂度为 O(n*n),超时。

思路二,结合思路一以及超时的  test case 发现,其实对于 A[i] 无需检查 k 次,只需要检查 A[i] 和右边最近的可达终点的元素的距离是否小于 A[i] 值即可。

v[i] 表示在 nums[i...n]中,nums[i] 到达下一个可达终点元素的距离。

例如,nums[i]自身可达终点,则v[i] = 0;nums[i]不可达而nums[i+1]可达,则 v[i] = 1;nums[i]不可达而nums[i+k]才可达,则v[i] = k;

借组辅助表格 v ,A[i] 只需要检查 v[i+1] 是否小于 A[i] 就可以知道 A[i] 是否可达终点。

元素是否可达终点,所以只需要判断 A[i] 的最大可跳距离是否大于最近可行元素即可,最近可行元素之后的情况无需考虑。思路二 实际就是贪心思路(Greedy),题目也是归类到 Greedy 下面的,吻合。

     bool canJump(vector<int>& nums) {

         if (nums.size() < ) {
return true;
} vector<int> v(nums.size()); long len = nums.size();
if (nums[len - ] > ) {
v[len - ] = ;
}else{
v[len - ] = ;
} for (int i = (int)nums.size()-; i >= ; i--) {
if (nums[i] > v[i+]) {
v[i] = ;
}else{
v[i] = v[i+] + ;
}
} return (v[] == ) ? true : false;
}

[LeetCode] 55. Jump Game 解题思路的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] 134. Gas Station 解题思路

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  3. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  4. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. 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 ...

  7. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  8. 【LeetCode】55. Jump Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  9. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

随机推荐

  1. linux 声音大小调整的命令

    alsamixer 输入上面的命令 回车即可看到图形界面,界面如下 ┌──────────────────────────── AlsaMixer v1.0.27.1 ──────────────── ...

  2. IPython notebook 使用介绍

    参考资料: http://mindonmind.github.io/2013/02/08/ipython-notebook-interactive-computing-new-era/ http:// ...

  3. nginx 3.nginx+fastcgi

    死磕nginx 3.nginx+fastcgi 互联网服务器有个非常典型的架构lamp(linux+apache+mysql+php),由于其开源和强大的兼容性而风靡一时,不过随着nginx的横空出世 ...

  4. wildfly部署solr.war

    1.添加solr/home配置,有两种途径: 一个是修改solr.war包的web.xml,路径如下:solr-4.7.2.rar\WEB-INF\web.xml,添加如下内容:

  5. MySQL数据库备份与恢复方法(转)

    来源于:http://www.jb51.net/article/25686.htm 网站数据对我们对站长来说都是最宝贵的,我们平时应该养成良好的备份数据的习惯.     常有新手问我该怎么备份数据库, ...

  6. 迁移/home目录至新硬盘分区总结--无备份情况下

    搞了一天,终于成功迁移.由于一开始就没备份过程实在很曲折. 希望本篇对那些没有备份习惯的朋友们有所帮助. 准备工作: sudo vim /etc/fstab 在文件中加入: /dev/sdb8     ...

  7. 将图片转换为Base64

    string Imagefilename   硬盘路径 protected string ImgToBase64String(string Imagefilename) { try { Bitmap ...

  8. RBAC角色权限控制

    RBAC角色权限控制 1. user (用户表) *  用户的基本信息(mid:用户信息id  如图) 2. node (节点表) * 页面(模块\控制器\方法) 3. role_node(角色.节点 ...

  9. redux-simple 简化版的redux

    作为react的粉丝,当然要吐槽一下react组件通信问题.react的单向数据流是组件通信的一大阻碍,只允许父组件向子组件传值,子组件向父组件传值只能通过父组件向子组件传递回调函数实现.如果在深层次 ...

  10. 三维FEM的刚度矩阵数量级

    刚刚在调试三维的FEM代码,随手把全局刚度矩阵在FULL的情况下保存到了dat文件里. 注意,这个仅仅是一个半径为十的球的刚度矩阵 居然3.6G!!吓坏了!!截个图,留个纪念.还是老老实实用spars ...