题目:

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.

Your goal is to reach the last index in the minimum number of jumps.

For example:

Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step
from index 0 to 1, then 3 steps to the last index.)

题意:

给定一个非负整数数组。给定的初始化位置在数组的起始位置。

数组中的每一个元素代表着你能都在此位置跳跃的最大的距离。

你的目标是用最少的跳跃数达到数组的末尾。

比方:给定A = [2,3,1,1,4]

达到数组尾部的最小的跳跃步数为2。(用1步从索引 0 到 1, 接着用3步到达结尾索引。)

算法分析:

该题思想主要是。扫描数组,以确定当前最远能覆盖的节点。放入maxreach。

然后继续扫描,直到当前的路程超过了上一次算出的覆盖范围reach,那么更新覆盖范围,同一时候更新条数,由于我们是经过了多一跳才干继续前进的。

形象地说,这个是在争取每跳最远的greedy.

* ret:眼下为止的jump数





    * curRch:从A[0]进行ret次jump之后达到的最大范围





    * curMax:从0~i这i+1个A元素中能达到的最大范围

    

    * 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素。因此须要添加一次jump,使之达到





    * 记录的curMax。

AC代码:

/**
* ret:眼下为止的jump数 * curRch:从A[0]进行ret次jump之后达到的最大范围 * curMax:从0~i这i+1个A元素中能达到的最大范围 * 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素,因此须要添加一次jump。使之达到 * 记录的curMax。 */
public class Solution
{
public int jump(int[] nums)
{
int ret = 0;
int curMax = 0;
int curRch = 0;
for(int i = 0; i < nums.length; i ++)
{
if(curRch < i)
{
ret ++;
curRch = curMax;
}
curMax = Math.max(curMax, nums[i]+i);
}
return ret;
}
}

[LeetCode][Java] Jump Game II的更多相关文章

  1. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  2. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

  3. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

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

  4. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  5. LeetCode 045 Jump Game II

    题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...

  6. Java for LeetCode 045 Jump Game II

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

  7. [LeetCode] 45. Jump Game II 解题思路

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

  8. LeetCode 45 Jump Game II(按照数组进行移动)

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  9. [LeetCode] 45. Jump Game II 跳跃游戏之二

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

随机推荐

  1. ORA-39126: Worker unexpected fatal error in KUPW$WORKER.PUT_DDLS

    末尾加上EXCLUDE=STATISTICS /home/opt/oracle/11g/bin/expdp user/password directory=backup2 network_link=d ...

  2. NSFileHandle类

    Objective-C使用NSFileHandle类对文件进行基本操作,IOS文件操作 NSFileHandle类中得方法可以对文件进行基本的读写,偏移量的操作.NSFileHandle基本步骤:1. ...

  3. Python学习笔记(1)——Python的概述(Python的环境、变量、数据类型、基本运算)

    Table of Contents 1. Python概述 1.1. Python基础知识 1.2. 运行环境 1.3. Python的格式 1.4. Python的变量. 2. Python的数据类 ...

  4. 「 Luogu P1122 」 最大子树和

    # 题目大意 真讨厌题面写的老长老长的. 这个题的意思就是给定一棵无根树,每个节点都有一个美丽值(可能是负数),可以删掉一些边来删除某些点,现在要求你求出可以删掉任意条边的情况下,这个树上的剩余节点的 ...

  5. NOIP专题复习3 图论-强连通分量

    目录 一.知识概述 二.典型例题 1.[HAOI2006]受欢迎的牛 2.校园网络[[USACO]Network of Schools加强版] 三.算法分析 (一)Tarjan算法 (二)解决问题 四 ...

  6. Anaconda基本用法

    Anaconda基本用法 conda info --envs/(-e) // 查看当前的环境变量 conda create -n py36(环境的名称,随意) python=3.6(指定版本) //  ...

  7. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  8. 将文件大小kb转换成M

    得到文件的大小的一般是直接到得到的是文件的字节大小,也就是kb,我们有的时候需要做单位换算成B或者M, 下面方法只是换成M,没有到G, 有更好的方法,请随时沟通,以便交流学习,谢谢. public s ...

  9. 【Codeforces 1042D】Petya and Array

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 把a[i]处理成前缀和 离散化. 枚举i从1..n假设a[i]是区间和的a[r] 显然我们需要找到a[r]-a[l]<t的l的个数 即a ...

  10. Python xml文件处理

    什么是XML文件? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,具体如 ...