[LeetCode#55, 45]Jump Game, Jump Game II
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的更多相关文章
- LeetCode 55. 跳跃游戏(Jump Game)
题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 前端与算法 leetcode 350. 两个数组的交集 II
目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- Leetcode之二分法专题-275. H指数 II(H-Index II)
Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
随机推荐
- Java——(四)Collection之Set集合TreeSet类
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- TreeSet类 TreeSet是SortedSet接口的实现类,正如SortedSet名字所暗 ...
- Less 关于css hack的写法
由于工作需要,最近一直在弄css转写less,遇到最多的问题就是 hack的写法,一些IE的hack,less不支持编译: 常见的不支持的hack如下: IE的滤镜写法 \9\0 IE8部分支持 ...
- 转载:修改xshell中文乱码的问题(管用)
执行echo $LANG命令输出的是当前的编码方式,执行locale命令得到系统中所有可用的编码方式.要让Xshell不显示乱码,则要将编码方式改为UTF-8. 在Xshell中[file]-> ...
- HTML5 WebAudioAPI(四)--绘制频谱图2
绘制分析器数组所有数据.本文内容,承接上文 1.800宽度绘制 var url='../content/audio/海阔天空.mp3'; if (!window.AudioContext) { ale ...
- Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
canvas绘制图片,由于浏览器的安全考虑,如果在使用canvas绘图的过程中,使用到了外域的图片资源,那么在toDataURL()时会抛出安全异常: Uncaught SecurityError: ...
- ORA-01653:表空间扩展失败的问题
以下内容来源于ORA-01653:表空间扩展失败的问题 今天发现,原来设备的数据表空间只有5M,已经满了,上网去找发现要进行扩展空间. 一.脚本修改方式: ----查询表空间使用情况---使用DB ...
- 【转】 iOS-Core-Animation-Advanced-Techniques(七)
高效绘图.图像IO以及图层性能 高效绘图 原文:http://www.cocoachina.com/ios/20150106/10840.html 不必要的效率考虑往往是性能问题的万恶之源. ——Wi ...
- SVN Global ignore pattern 忽略文件正则后缀
*.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo __pycache__ *.rej *~ #*# .#* .*.swp .DS_St ...
- JS & JQuery 动态添加 select option
因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...
- MATLAB中mexFunction函数的接口规范
MEX文件的调用极为方便,其调用方式与MATALAB的内建函数完全相同,只需要在命令窗口内输入对应的文件名称即可. C语言MEX程序代码文件有计算子例程(Computational routine)和 ...