Jump Game II 解答
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 解答的更多相关文章
- 57. Jump Game && Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【LeetCode】45. Jump Game II
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 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 ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [leetcode解题记录]Jump Game和Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
随机推荐
- c++ 12
一.模板与继承 1.从模板类派生模板子类 2.为模板子类提供基类 二.容器和迭代器 以链表为例. 三.STL概览 1.十大容器 1)向量(vector):连续内存,后端压弹,插删低效 2)列表(lis ...
- jumpGate部署
preface statement: manageing OpenStack & SoftLayer resource with Jumpgate 1,forward; Imagine a w ...
- 关于Tcp三次握手的思考
一.为什么不能使两次握手,两次握手就应该可以保证线路的畅通? 1) 只能建立一个方向的连接,称为半连接 记住TCP是全双工的. A向B发出请求,同时收到B的确认,这时只有A.B知道A到B的连接成功了. ...
- paip.输入法编程---智能动态上屏码儿长调整--.txt
paip.输入法编程---智能动态上屏码儿长调整--.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csd ...
- JUnit三分钟教程 ---- 实际应用
JUnit三分钟教程 ---- 实际应用 摘自http://lavasoft.blog.51cto.com/62575/65775 接上文"JUnit三分钟教程 ---- 快速起步&qu ...
- shell编程while
脚本编程: 顺序结构 选择结构 if case 循环结构 for while until whil ...
- Hibernate(四)——缓存策略+lazy
Hibernate作为和数据库数据打交道的框架,自然会设计到操作数据的效率问题,而对于一些频繁操作的数据,缓存策略就是提高其性能一种重要手段,而Hibernate框架是支持缓存的,而且支持一级和二级两 ...
- 跟我学系列教程——《13天让你学会Redis》火热报名中
学习目标 每天2小时,13天让你学会Redis. 本课程针对Redis新手,甚至连Redis是什么都没有听说过的同学.课程会具体介绍Redis是什么以及为什么要使用Redis,结合项目实践旨在让学生从 ...
- SQLLoader4(数据文件中的列与表中列不一致情况-filler)
A.数据文件中字段个数少于表中列字段个数,但数据文件中缺少的列,在表定义中可以为空.----- 这种情况是比较简单的,只需要将数据文件中数据对应的列的名字写到控制文件中即可.因为SQL*Loader是 ...
- 不能修改“System Roots”钥匙串 即下载的.cer 文件添加不到钥匙串
双击提示 :不能修改“System Roots”钥匙串要更改根证书是否会被信任,请在“钥匙串访问”中打开它,然后修改它的信任设置. 解决办法:添加到 登录或显示LOGIN的 keychain(记 ...