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. 新概念英语(1-32)A fine day

    新概念英语(1-33)A fine day Where is the Jones family? It is a fine day today. There are some clouds in th ...

  2. Mego开发文档 - 从EF6/EFCore迁移到Mego

    从EF6/EFCore迁移到Mego框架 如果您有EntityFragmework6或EntityFragmeworkCore的开发经验,在首次接触Mego框架时会发现这两个框架非常相似,本文将帮忙您 ...

  3. Python系列之 - 面向对象(1)

    python是一门面向对象的编程语言,python中的一切均是对象. 有对象就提到类,对象和类就像是儿子和老子的关系,是不可分的一对. 什么是类     类就是具有一些共同特性的事物的统称.好比人类, ...

  4. Iframe父页面与子页面之间的相互调用

    iframe元素就是文档中的文档. window对象: 浏览器会在其打开一个HTML文档时创建一个对应的window对象.但是,如果一个文档定义了一个或者多个框架(即:包含一个或者多个frame或者i ...

  5. Spring MVC【入门】就这一篇!

    MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...

  6. Spring-Cloud(三)Eureka注册中心实现高可用

    前言: spring-cloud为基础的微服务架构,所有的微服务都需要注册到注册中心,如果这个注册中心阻塞或者崩了,那么整个系统都无法继续正常提供服务,所以,这里就需要对注册中心进行集群,换言之,高可 ...

  7. leetcode 717. 1-bit and 2-bit Characters -easy

    https://leetcode.com/problems/1-bit-and-2-bit-characters/description/ We have two special characters ...

  8. mysql列约束

    列属性(约束)1: 是否允许为空(not null)  --not null不允许为空create table t_1(    a tinyint(3) zerofill not null,    b ...

  9. tarjan——cogs 1298 通讯问题

    1298. 通讯问题 ★   输入文件:jdltt.in   输出文件:jdltt.out   简单对比 时间限制:1 s   内存限制:128 MB [题目描述] 一个篮球队有n个篮球队员,每个队员 ...

  10. bzoj 5212: [Zjoi2018]历史

    Description 九条可怜是一个热爱阅读的女孩子. 这段时间,她看了一本非常有趣的小说,这本小说的架空世界引起了她的兴趣. 这个世界有n个城市,这n个城市被恰好n?1条双向道路联通,即任意两个城 ...