55. Jump Game(中等)

时间:2022-09-18 23:24:09

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&period; Jump Game leetcode

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

  2. &lbrack;Leetcode&rsqb;&lbrack;Python&rsqb;55&colon; Jump Game

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

  3. Leetcode 55&period; Jump Game &amp&semi; 45&period; Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

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

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

  5. 刷题55&period; Jump Game

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

  6. &lbrack;LeetCode&rsqb; 55&period; Jump Game 跳跃游戏

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

  7. Leetcode 55&period; Jump Game

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

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

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

  9. 55&period; Jump Game

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

随机推荐

  1. hexo 本地local4000打不开解决方法

    错误:Cannot GET /spadesq.github.io/ (注:spadesq.github.io是原来放hexo文件夹的名字) 由于我后来把hexo文件夹搬迁到别处,但我发现打开本地,地址 ...

  2. vue 一些开发姿势

    .vue : <template></template><script></script> .js :import Vue from 'vue'

  3. &lbrack;转&rsqb;Android中内存占用的含义:(VSS,PSS,RSS,USS)

    Android中内存占用的含义:(VSS,PSS,RSS,USS) 作者: andforce 分类: 安卓系统 发布时间: 2013-09-07 00:03 ė1,915 浏览数 6没有评论 在eng ...

  4. 2014年辛星完全解读Javascript第二节

    本小节我们讲解一下Javascript的语法,虽然js语言非常简单,它的语法也相对好学一些,但是不学总之还是不会的,因此,我们来一探究竟把. ********注释************* 1.我们通 ...

  5. listView 单选实现

    上一篇知道可以使用android自带的listview的chiocemode的单选模式实现.但那个布局是系统自带的checkedTextView,有时候我们需要自己实现布局,那么下面我们开始实现   ...

  6. mysql sql 基础总结

    1 mysql top n使用 select * from table limit n; 2    统配符使用必须和like结合使用 like % 通配符 描述 % 替代一个或多个字符 _ 仅替代一个 ...

  7. 【原创】Nginx&plus;PHP-FPM优化技巧总结

    php-fpm的安装很简单,参见PHP(PHP-FPM)手动编译安装.下面主要讨论下如何提高Nginx+Php-fpm的性能. 1.Unix域Socket通信 之前简单介绍过Unix Domain S ...

  8. &lbrack;C&rsqb;&lbrack;代码实例&rsqb;交换指向常量的二级指针的位置

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> ...

  9. OA流程分析

    OA流程分析: 1. 流程定义:可视化可拖拽的流程环节设置,流程定义完成后保存在数据表中,字段有流程ID,名称,流程流转环节. 2. 画业务表单,新建业务数据表. 3. 表单数据填好后,启动流程:

  10. UFLDL 教程学习笔记(四)主成分分析

    UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...