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[0] = 2, 代表 index = 1, 2 的元素我们都可以跳到(我原来以为得必须跳到 index = 2 的位置呢). 我们把能达到的最远索引保存在变量reach中. A[1] = 3, A[2] = 1, 那么选最大的, 则 reach = i + A[i] = 1 + 3.

说是贪心法.

精简的意思就是:

reach 是前沿阵地位置, i 是后方补给当前所在位置, 补给只在 reach 所定的安全区域逐步前行, i 能否到达数组最后一个元素, 完全取决于 reach 所能到达的位置. 但 i 在前行的过程中, 符合条件的话可以更新 reach. 有时候,虽然 i 还没到达数组最后元素, 但当前的reach >= n - 1了, i 就不需要向前走了(循环被 break), 因为reach已经表明共产主义肯定能实现, i 显然就没有向下走的必要了.

人家想法,自己代码:

牛就牛在这想法是\(O(n)\)的,而笨想法是\(O(n^2)\)的.

\(O(n)\) time, \(O(1)\) extra space.

bool canJump(vector<int>& A) {
const int n = A.size(); int i = 0;
// 扫描reach范围内, i + A[i] 所能到达的最远位置.
// 若能超过当前reach,则更新reach.
for (int reach = 0; i < n && i <= reach; i++) {
reach = max(i + A[i], reach);
if (reach >= n - 1)
return true;
}
// 2个事能导致退出循环, 既上面的循环条件
// 不等于n, 只能因为 reach < i, reach 鞭长莫及了
return i == n;
}

55. Jump Game(中等)的更多相关文章

  1. 55. Jump Game leetcode

    55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...

  2. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

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

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

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

  5. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  6. [LeetCode] 55. Jump Game 跳跃游戏

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

  7. Leetcode 55. Jump Game

    我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...

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

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

  9. 55. Jump Game

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

随机推荐

  1. 路由测试-lee

    //get 路由 Route::get('/', 'WelcomeController@index'); Route::get('home', 'HomeController@index'); //路 ...

  2. CentOS7为firewalld添加开放端口

    运行.停止.禁用firewalld 启动:# systemctl start  firewalld 查看状态:# systemctl status firewalld 或者 firewall-cmd ...

  3. Django快速入门

    Django 是用 Python 写的一个自由和开放源码 web 应用程序框架.web框架是一套组件,能帮助你更快.更容易地开发web站点.当你开始构建一个web站点时,你总需要一些相似的组件:处理用 ...

  4. CWMP开源代码研究——cwmp移植

    原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 声明:本系列涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅 ...

  5. 使用NPOI-创建Excel

    这里简单的使用一下NPOI ,什么是NPOI? 既然你已经在需要使用了,就一定知道NPOI是干什么用的了. 开始正题吧. 我用控制台程序来给大家演示一下: 一.创建控制台程序 自行脑补 二.添加NPO ...

  6. 阿里云、腾讯云开通端口 telnet不通的原因

    1.安全组是否已经开通相对应的端口: 阿里云:https://help.aliyun.com/document_detail/25471.html 腾讯云:http://bbs.qcloud.com/ ...

  7. Temple Build~dp(01背包的变形)

    The Dwarves of Middle Earth are renowned for their delving and smithy ability, but they are also mas ...

  8. Joomla!3.7.0 Core SQL注入漏洞动态调试草稿

    参考joolma的mvc框架讲解:http://www.360doc.com/content/11/1219/18/1372409_173441270.shtml 从这个页面开始下断点:Joomla_ ...

  9. [NOI 2011]阿狸的打字机

    Description 题库链接 给你 \(n\) 个单词, \(m\) 组询问,每组询问形同 \((x,y)\) ,询问 \(x\) 串在 \(y\) 串中出现多少次. \(1\leq n,m\le ...

  10. [POI2007]POW-The Flood

    题目描述 给定一张地势图,所有的点都被水淹没,现在有一些关键点,要求放最少的水泵使所有关键点的水都被抽干 输入输出格式 输入格式: In the first line of the standard ...