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. tomcat结合nginx使用 基础教程

    相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...

  2. css 实现文字过长变成省略号(包含单行的and多行的)

    单行的比较简单  但是必须条件同时满足 <DIV STYLE="width: 120px; height: 50px; border: 1px solid blue;overflow: ...

  3. spring配置文件位置

    参考http://name327.iteye.com/blog/1628884

  4. TextView drawablePadding没有效果

    1.当TextView 设置宽度设置为match_parent的时候 TextView drawablePadding没有效果 ,字设置了center位置,但是和左边的图片离开很远 2.当TextVi ...

  5. js页面跳转

    js方式的页面跳转1.window.location.href方式    <script language="javascript" type="text/java ...

  6. Php RSS

    RSS 聚合最近非常流行,因此至少对 RSS 及其工作方式有所了解是一名 PHP 开发人员的迫切需要.本文介绍了 RSS 基础知识.RSS 众多用途中的一些用途.如何使用 PHP 从数据库创建 RSS ...

  7. java编程小技巧

    1.缩进与反缩进 缩进:tab 反缩进:shift+tab 2.整段注释和取消整段注释 整段注释:ctrl+shift+/ 取消整段注释:ctrl+shift+\

  8. C++拾遗(十三)友元和嵌套类

    友元类 使用友元的场合: 1.两个类既不是is-a关系也不是has-a关系,但是两个类之间又需要有联系,且一个类能访问另一个类的私有成员和保护成员. 2.一个类需要用到另外多个类的私有成员. C++p ...

  9. 《Linux内核分析》 week4作业-使用嵌入式汇编调用一个系统调用

    一.fork的嵌入式汇编执行 #include <stdio.h> #include <unistd.h> int main(){ pid_t pid; asm volatil ...

  10. [转]Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile

    http://blog.csdn.net/stpeace/article/details/39534361 进程间的通信方式有很多种, 上次我们说了最傻瓜的“共享外存/文件”的方法. 那么, 在本文中 ...