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. Oracle判断语句集合(转载)

    SELECT decode(sign(to_date('2008-05-01', 'yyyy-MM-dd') -                   to_date('2008-03-01', 'yy ...

  2. 恢复Linux下被误删除的文件(笔记)

    恢复Linux下被误删除的文件 [root@xuegod63 ~]# mount /dev/cdrom /mnt/ 分一个区:sda4  查找:extundelete 分一个区:sda4  [root ...

  3. hdu 3232 Crossing Rivers(期望 + 数学推导 + 分类讨论,水题不水)

    Problem Description   You live in a village but work in another village. You decided to follow the s ...

  4. jquery 弹出框 showDialog.js具体用法

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABWwAAAImCAIAAABID1T7AAAgAElEQVR4nO3d329c52Hgff1HvPCNLw

  5. redis知识

    http://www.cnblogs.com/moon521/p/5301895.html 菜鸟教程:http://www.runoob.com/redis/redis-tutorial.html

  6. Spark Streaming--实战篇

    摘要:      Sprak Streaming属于Saprk API的扩展,支持实时数据流(live data streams)的可扩展,高吞吐(hight-throughput) 容错(fault ...

  7. Android——编译odex保护

    编译过android源代码的可能试验过改动编译类型.android的初始化编译配置可參考Android--编译系统初始化设置 一.TARGET_BUILD_VARIANT=user 当选择的编译类型为 ...

  8. Sql存储过程解密方法

    在网上查到这样一个存储过程解密的方法,用起来简单,收藏到这里: )) AS ------------------------sql2000大于40000的----------------- --原作: ...

  9. asp.net mvc输出自定义404等错误页面,非302跳转

      朋友问到一个问题,如何输出自定义错误页面,不使用302跳转.当前页面地址不能改变. 还要执行一些代码等,生成一些错误信息,方便用户提交反馈. 500错误,mvc框架已经有现成解决方法: filte ...

  10. ORA-14450

    ORA-14450 attempt to access a transactional temp table already in use Cause: An attempt was made to ...