[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,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
随机推荐
- [转] 怎样在Ubuntu 14.04中搭建gitolite git服务器
相比gitosis,gitolite的功能更为强大,支持对权限的细分控制,学习一下在最新版 的ubuntu 14.04 LTS中搭建gitolite服务器是非常有必要的,嘿嘿,一会属于我们自己的Git ...
- 小结 iOS 中的 copy
预备知识 : 内存的栈区 : 由编译器自动分配释放, 存放函数的参数值, 局部变量的值等. 其 操作方式类似于数据结构中的栈. 内存的堆区 : 一般由程序员分配释放, 若程序员不释放, 程序结束时可能 ...
- Windows开启Telnet
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/4301513.html ...
- 10.29 morning
WPS转word太丑了 凑合看喽 第二题 [题目描述] 给你两个日期,问这两个日期差了多少毫秒. [输入格式] 两行,每行一个日期,日期格式保证为“YYYY-MM-DD hh:mm:ss ”这种形式. ...
- 10.4 noip模拟试题
题目名称 PA 青春 三部曲 名称 huakai taritari truetears 输入 huakai.in taritari.in truetears.in 输出 huakai.out tari ...
- java判断字符串是否为空的方法总结
http://blog.csdn.net/qq799499343/article/details/8492672 以下是java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观 ...
- ajax跨域传值
<script type="text/javascript"> function xmlpage(){ $.ajax({ url:'http://localhost/3 ...
- asp.net mvc 部署在IIS7.5上出现的[没有相关的源行]错误的解决办法
今天在IIS7.5上部署一个MVC小项目的时候出现以下错误:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET File ...
- 浅谈KMP算法及其next[]数组
KMP算法是众多优秀的模式串匹配算法中较早诞生的一个,也是相对最为人所知的一个. 算法实现简单,运行效率高,时间复杂度为O(n+m)(n和m分别为目标串和模式串的长度) 当字符串长度和字符集大小的比值 ...
- windows core audio apis
这个播放流程有一次当初不是很理解,做个记录,代码中的中文部分,原文档是有解释的:To move a stream of rendering data through the endpoint buff ...