Question

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

Solution

This problem can be transformed into "Given step n, what is the largest distance that you can reach?"

Beside maintaining an array to record farest reachable position, we need also know how farest current jump can reach.

Example

 public class Solution {
public int jump(int[] nums) {
if (nums == null || nums.length < 1)
return -1;
int length = nums.length;
// Three variables
// maxReach: max reachable position for current index and previous indexes
// maxJumpReach: max position that can be reachable by n jump steps
// jump: jump steps
int maxReach = nums[0];
int maxJumpReach = 0;
int jump = 0;
for (int i = 0; i < length && maxJumpReach <= (length - 1); i++) {
if (i > maxJumpReach) {
jump++;
maxJumpReach = maxReach;
}
maxReach = Math.max(maxReach, i + nums[i]);
}
return jump;
}
}

Jump Game II 解答的更多相关文章

  1. 57. Jump Game && Jump Game II

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

  2. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  3. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

  4. LeetCode: Jump Game II 解题报告

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

  5. 【LeetCode】45. Jump Game II

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

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

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

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

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

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

  9. [leetcode解题记录]Jump Game和Jump Game II

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

随机推荐

  1. 优化C#程序的48种方法

    一.用属性代替可访问的字段 1..NET数据绑定只支持数据绑定,使用属性可以获得数据绑定的好处: 2.在属性的get和set访问器重可使用lock添加多线程的支持. 二.readonly(运行时常量) ...

  2. UML建模工具-火龙果软件

     官网地址:http://code.uml.com.cn/index.asp     Bridge桥梁模式    (待逆向) 桥梁模式,通过增加一个类,将抽象部分与它的实现部分分离,使它们都可以独立 ...

  3. add BOM to fix UTF-8 in Excel

    fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

  4. 基础总结篇之二:Activity的四种launchMode

    合抱之木,生於毫末:九層之台,起於累土:千里之行,始於足下.<老子> 今天在社区看到有朋友问“如何在半年内成为顶级架构师”,有网友道“关灯睡觉,不用半年的...”,的确,做梦还来的快一些. ...

  5. PHP二分查找(递归和循环)

    二分查找可以通过递归和循环来实现, 思路如下: 将要查找的数和中间数进行比较, 如果相等,则表示找到,返回下标 如果要查找的数小于中间这个数,则说明要查找的数分布在数组左边,修改right边界,使其等 ...

  6. Analyzing the Analyzers 分析分析师 —— 数据科学部门如何建

    很多牛逼的公司都宣称在建立数据科学部门,这个部门该如何组建,大家都在摸石头过河. O‘reilly Strata今年 六月份发布了报告 <Analyzing the Analyzers>, ...

  7. Android Training: 设备管理

    Android 设备管理 Android2.2 通过Android设备管理API提供对企业级应用的支持.设备管理API在系统级别提供了设备管理特性.这些API可以在企业环境下,需要对员工设备进行控制时 ...

  8. UIScrollView 代理方法

    在使用UIScrollView和它的子类UITableView时,有时需要在不同操作状态下,做不同的响应. 如何截获这些状态,如正在滚动,滚动停止等,使用UIScrollViewDelegate_Pr ...

  9. iOS系统相册的有关操作

    iOS中,我们选择相册中的资源和调用摄像头可以使用 :UIImagePickerController类来完成,不使用UI我们可以通过:ALAssetsLibrary类来使用相册资源. 一. ALAss ...

  10. 循环小数 UVa202

    输入整数a和b(0<=a<=3000,1<=b<=3000),输出a/b的循环小数表示以及循环节长度. 例如,a=5,b=43,小数表示为0.(1162790697674418 ...