The problem:

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.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false.

My analysis:

This question is very easy! You just need to understand clearly about the underlying invariant for reaching each element in the Array.
The key idea: max_reach indicates the maximum position that could be reached now.
At each position, we do following things:
1. test if the current element could be reached by the previous elements(max_reach).
if (max_reach < i) {
return false;
}
2. if the current element could be reached, we check if we need to update the max_reach.
iff max_reach < i + A[i], it indicates the max_reach element could be further.
iff max_reach >= i + A[i], we need to do nothing.
max_reach = Math.max(max_reach, i + A[i]);

My solution:

public class Solution {
public boolean canJump(int[] A) {
if (A == null || A.length == 1)
return true;
int max_reach = 0;
for (int i = 0; i < A.length; i++) {
if (max_reach < i) {
return false;
} else{
max_reach = Math.max(max_reach, i + A[i]);
}
}
return true;
}
}

Problem 2:

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

My analysis:

The idea behind this problem is easy.
key idea: use a max_reach[] array to record the maximum position the elements(before and include element i) could reach.
max_reach[i] : indicate how far we can reach from the elemenets(before and include element i).
for (int i = 0; i < A.length; i++) {
if (pre_max < i)
return - 1;
if (A[i] + i > pre_max) {
max_reach[i] = A[i] + i;
pre_max = A[i] + i;
} else {
max_reach[i] = pre_max;
}
}
1. At each position, we have a reach range: [cur, max_reach[i]]. In the range, we would like to step farest(to quickly reach the last element). The range's farest reach is stored at max_reach[max_reach[i]], cause we only record the farest position we could reach at max_reach array.
A = [2, 3, 1, 1, 4] max_reach = [2, 4, 4, 4, 8]
At A[0], the reach range is [0, 2]. The farest position we could reach through the range is record at max_reach[2].
Note the invariant, it is very very interesting!
We make our decison not based on the farest position(range) the current element could reach, but on the farest position(range) the current range could reach!
How to get the range's information? we use max_reach[] array, it records the cultimative information in the array.
!!!Range computation. 2. There is a little pitfall we should take care, that is we only need to reach the last element! Don't continue the jump out of the array.
while (temp_reach < A.length - 1) {
temp_reach = max_reach[temp_reach];
count ++;
}

My solution:

public class Solution {
public int jump(int[] A) {
if (A == null || A.length == 0)
return -1;
int[] max_reach = new int[A.length];
int pre_max = 0;
int count = 0;
int temp_reach;
for (int i = 0; i < A.length; i++) {
if (pre_max < i) //if we could not
return - 1;
if (A[i] + i > pre_max) {
max_reach[i] = A[i] + i;
pre_max = A[i] + i;
} else {
max_reach[i] = pre_max;
}
}
temp_reach = 0;
while (temp_reach < A.length - 1) {
temp_reach = max_reach[temp_reach]; //except for last element, there is no possible: max_reach[temp_reach] = temp_reach <we have a detection ahead!>
count ++;
}
return count;
}
}

[LeetCode#55, 45]Jump Game, Jump Game II的更多相关文章

  1. LeetCode 55. 跳跃游戏(Jump Game)

    题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...

  2. LeetCode(45): 跳跃游戏 II

    Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  3. LeetCode (45) Jump Game II

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

  4. [LeetCode] 55. Jump Game 跳跃游戏

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

  5. 前端与算法 leetcode 350. 两个数组的交集 II

    目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...

  6. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  7. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  8. Leetcode之二分法专题-275. H指数 II(H-Index II)

    Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...

  9. Leetcode之回溯法专题-90. 子集 II(Subsets II)

    Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...

随机推荐

  1. Java NIO 学习笔记

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3344148.html ...

  2. win 10 安装 mysql解压版 步骤

    参考资料:win 10 安装 mysql 5.7 网址:http://blog.sina.com.cn/s/blog_5f39af320102wbk0.html 本文参考上面的网址的教程,感谢作者分享 ...

  3. win7、xp下Meclipse SVN用户名修改

    Meclipse SVN用户名修改,在网上查找后发现如下方法: 1.查看你的Eclipse中使用的是什么SVNInterface windows>preference>Team>SV ...

  4. jsp-文件的上传(转).

    该程序的主要代码,我引用网友的,并做了一些改进.上这个帖子的原因之一,是为了修正之前自己的一些误解. 概述: 一些网友,包括我,也曾经试图通过 input type 为 file的控件,获取其文件的完 ...

  5. PHP代码批量加密

    <?php error_reporting(E_ALL); ini_set('display_errors','1'); //批量加密码当前目录 $dirnow = getcwd(); $dir ...

  6. php+支付宝整合

    CREATE TABLE IF NOT EXISTS `alipay_order` ( `id` ) unsigned NOT NULL auto_increment, `orderid` ) NOT ...

  7. iOS多线程的初步研究(八)-- dispatch队列

    GCD编程的核心就是dispatch队列,dispatch block的执行最终都会放进某个队列中去进行,它类似NSOperationQueue但更复杂也更强大,并且可以嵌套使用.所以说,结合bloc ...

  8. 用html/css做的一个登入小界面(图片瀑布流)

    一个登入效果简易图:(色彩搭配有点乱,嘻嘻,可以在代码处改成自己喜欢的颜色) css样式的代码: style.css: @charset "utf-8";/* CSS Docume ...

  9. JS获取IP

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js新浪多地域测试方法:http://int.dpool.s ...

  10. SQL语句 常用条件判断

    条件判断写法: 对每天记录执行操作时,判断所限制的条件-----> 操作符:                     =      <>(不匹配检查)       !=     &l ...