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. 升级 node 版本

    npm install -g n n stablen v0.10.26 n 0.10.26

  2. IDLHDF5读取与转换

    需求决定了动力,此时近凌晨一点,忙里偷闲,终于忙完了今天的“这点儿”事儿.参考帮助文档,从hdf的读写,捉摸hdf5的读写,总算弄明白了.稍作总结,以备候查. hdf5作为hdf数据的补充与升级,目前 ...

  3. 在企业级开发中使用Try...Catch...会影响效率吗?

    感谢神啊.上帝及老天爷让我失眠,才能够有了本篇文章. 记得不久之前,公司一同事曾经说过:“如果是Winform开发,由于程序是在本地,使用try...catch不会有太大性能问题,可是如果是在web服 ...

  4. SQL中使用的一些函数问题

    abs()取绝对值ceil()上取整floor()下取整initcap()使串中的所有单词的首字母变为大写substr()取子串 这些函数都是oracle的sql内置函数.

  5. IOS开发网络篇之──ASIHTTPRequest详解

    目录 目录 发起一个同步请求 创建一个异步请求 队列请求 请求队列上下文 ASINetworkQueues, 它的delegate提供更为丰富的功能 取消异步请求 安全的内存回收建议 向服务器端上传数 ...

  6. CSRF 攻击的应对之道

    转载自imb文库 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在 ...

  7. Strut2 采用token机制防御CSRF同时也可以防止表单重复提交

    一 未配置Struts2 token的情况下测试 1.从表单提交数据,可以从下图看出,快速点击保存按钮,请求提交了两次 2.检查post提交的数据中未含有token参数 3.查看数据列表,有重复数据 ...

  8. 仿小米网jQuery全屏滚动插件fullPage.js

    演 示 下 载   简介 如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容,显得格外的高端大气上档次.比如 iPhone 5C 的介绍页面,QQ浏 ...

  9. Linux系统、版本、CPU、内存查看、硬盘空间

    查看系统版本:lsb_release -a [root@localhost /]# lsb_release -a LSB Version:    :core-4.0-amd64:core-4.0-no ...

  10. 颤抖吧,Css3

    相关文章地址:http://sc.chinaz.com/info/140315283609.htm http://files.cnblogs.com/xinlinux/csshake.min.css ...