题目:

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;
}
}

最新文章

  1. BPM Domino集成解决方案
  2. Swift -Login(MVC 纯代码)
  3. 原创:Equinox OSGi应用嵌入Jersey框架搭建REST服务
  4. log4j输出日志到文件
  5. C语言简易文法(无左递归)
  6. 《Memcache学习总结》[PDF]发布
  7. b/s开发者的困境
  8. [转]在SqlServer 中解析JSON数据
  9. 用尽洪荒之力解决Apple Store ipv6审核通关---linux服务器支持ipv6
  10. Iaas概述
  11. 利用LRUMap 设计缓存
  12. Linux学习笔记20——第一个多线程程序
  13. 第一局 ThreeJS-开始
  14. BZOJ 1415: [Noi2005]聪聪和可可 [DP 概率]
  15. C#中引用变量是否应该加ref?
  16. Eclipse搭建服务器,实现与Android的简单通信
  17. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)
  18. swift中UIImageView的创建
  19. 多线程分段下载研究的python实现(一)
  20. 2018-2019-2 《网络对抗技术》Exp1 PC平台逆向破解 20165222

热门文章

  1. JavaSE-21 字符编码简介
  2. pytorch之Tensor与Variable的区别
  3. scrollfix.js插件:滚动固定在某个位置
  4. cookie和session的区别及session的生命周期
  5. Python之面向对象slots与迭代器协议
  6. Django之模板引擎(母版)
  7. LeetCode(29)Divide Two Integers
  8. UVA 230 Borrowers (STL 行读入的处理 重载小于号)
  9. bzoj3262陌上花开 cdq分治入门题
  10. pip提示Did not provide a commend