[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,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
随机推荐
- Bash关闭输出(关闭正确、错误输出)
利用&>重定向,不输出任何内容: echo hello &> /dev/null 关闭正确输出: echo hello 1> /dev/null 关闭错误输出: ec ...
- poj 3565 ants
/* poj 3565 递归分治 还有用KM的做法 这里写的分治 按紫书上的方法 不过那里说的有点冗杂了 可以简化一下 首先为啥可以分治 也就是分成子问题解决 只要有一个集合 黑白的个数相等 就一定能 ...
- TFS 服务器更换后工作区无法绑定
需要删除工作区,删除命令如下 tf workspace /delete 工作区名;创建的用户 /server:TFS服务器 例 tf workspace /delete WHQ-PC;whq /ser ...
- XFire构建服务端Service的两种方式(转)
XFire构建服务端service的两种方式,一是用xfire构建,二是和spring集成构建. 一,xifre构建,确保把xfire的jar包导入到工程中或classpath. 1,service的 ...
- 外部式css样式,写在单独的一个文件中
外部式css样式(也可称为外联式)就是把css代码写一个单独的外部文件中,这个css样式文件以“.css”为扩展名,在<head>内(不是在<style>标签内)使用<l ...
- async await的前世今生
async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...
- STL库list::sort()实现深度解析
原创,转载请注明出处:STL库list::sort()实现深度解析 list模板的定义以及一些基本成员函数的实现这里我就不赘述了,还不清楚的同学可以到网上查找相关资料或者直接查看侯捷翻译的<ST ...
- 常见ORACLE错误,及解决方案(遇则即时更新)
1.当登陆时提示“ORA-03113:通信通道的文件结束”时: 解决方案: 需在X:\oraclexe\app\oracle\product\10 ...
- Float之谜
先来看几个例子: public class Thirtyfirst1{ public static void main(String[] args){ int i = 2000000000; int ...
- 安装完 MySQL 后必须调整的 10 项配置
原文出处: mysqlperformanceblog 译文出处:开源中国 欢迎分享原创到伯乐头条 当我们被人雇来监测MySQL性能时,人们希望我们能够检视一下MySQL配置然后给出一些提高建议 ...