Jump Game | & ||
Jump Game |
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.
Example
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
分析:
使用一个variable来保存当前剩余的“可跳的步数”,如果小于等于0,则表示不能再往下跳了。
public class Solution {
/**
* @param A: A list of integers
* @return: The boolean answer
*/
public boolean canJump(int[] nums) {
if (nums == null) return false;
int stepsRemaining = ;
for (int i = ; i < nums.length; i++) {
stepsRemaining = Math.max(stepsRemaining - , nums[i]);
if (stepsRemaining <= && i != nums.length - ) return false;
}
return true;
}
}
Jump Game II
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.
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.)
分析:
当我们在第一个点“2”的时候,我们需要遍历所有能够到达的点, i.e., 3, 1,然后找出当我们到达第3个点的时候,从哪个点(3或者1)起跳剩余跳数最多。
public class Solution {
public int jump(int[] A) {
if (A == null || A.length <= ) return ;
int position = , totalJumps = ;
int remainingJumps = A[];
int jumps = A[];
while (position < A.length - ) {
// find max remaining jumps in the "jump range"
for (int i = ; i <= jumps; i++) {
if (position == A.length - ) return totalJumps;
remainingJumps = Math.max(A[position], remainingJumps - );
position++;
}
jumps = remainingJumps;
totalJumps++;
}
return totalJumps;
}
}
思路二:
假定我们从 index i 处起跳, 那么最多能够跳到 i + A[i] (代码中的currentFarthestPoint)这么远。那么我们需要在 i 到 i + A[i] 这个范围内找到一个点 p,使得如果我们从那个p点开始跳,它能够比任何一个在i 到 A[i] 这些点都能reach更远或者相等的点(代码中的nextFarthestPoint),并把那个最远的点作为下一次跳能够到达的最远点。
public class Solution {
public int jump(int[] A) {
if (A == null || A.length <= ) return ;
int total = , currentFarthestPoint = A[], i = ;
while (i < A.length) {
int nextFarthestPoint = ;
while (i <= Math.min(A.length - , currentFarthestPoint)) {
nextFarthestPoint = Math.max(nextFarthestPoint, A[i] + i);
i++;
}
total++;
currentFarthestPoint = nextFarthestPoint;
}
return total;
}
}
Jump Game | & ||的更多相关文章
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 55. Jump Game
我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- bug report: Jump to the invalid address stated on the next line at 0x0: ???
gdb或者vlagrind报告: ==14569== Jump to the invalid address stated on the next line ==14569== at 0x0: ??? ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
随机推荐
- UVA5870 乱搞 Smooth Visualization
#include<stdio.h> #include<string.h> #define maxn 1201 ][],s[maxn]; int col; int getmax( ...
- 常用JQuery插件
虽然自己也写过插件,但JQuery插件种类的繁多,大多时候,我还是使用别人写好的插件,这些都是我用了同类插件里较为不错的一些,今天就整理一下公开放出来. UI: jquery.HooRay(哈哈,自己 ...
- 【UVA 11401】Triangle Counting
题 题意 求1到n长度的n根棍子(3≤n≤1000000)能组成多少不同三角形. 分析 我看大家的递推公式都是 a[i]=a[i-1]+ ((i-1)*(i-2)/2-(i-1)/2)/2; 以i 为 ...
- Search everything 使用说明
Everything是速度最快的文件搜索软件,可以瞬间搜索到你需要的文件.
- #pragma预处理实例
1.#include <stdio.h>#if defined(ANDROID20) #pragma message("Compile Android SDK 2.0... ...
- Laravel教程 二:路由,视图,控制器工作流程
Laravel教程 二:路由,视图,控制器工作流程 此文章为原创文章,未经同意,禁止转载. View Controller 上一篇教程我们走了那么长的路,终于把Laravel安装好了,这一篇教程我们就 ...
- IDEA 从SVN检出项目相关配置
1.新建好一个工程,然后通过SVN检出项目 2.检出后一般tomcat的环境是配置好的,点击上方Project Structure按钮,弹出窗体,查看Project项,一般没问题,如果要配置就配置Pr ...
- 离线渲染中的不规则光源(Meshlight)
之前一直在考虑这样一个问题,在实际生活中的光源都是有体积的,但是图形学中,很多时候我们用简单的点光源,面光源,或者方向光来模拟实际生活中这些光源,势必会产生一些误差,同时导致很多效果不好做.那么在离线 ...
- CruiseControl.NET学习总结(转载)
前些日子,总结了一个NAnt的学习总结.后来就放下了,松散了一阵子.CruiseControl.NET(以下称CC.NET),是我在学习完NAnt以后才开始看的,当时学起来就是在网上疯狂的找资料.现在 ...
- jsp页面显示数据库乱码
如何页面是utf-8,数据库也是的话,页面显示数据库乱码的话,就是数据库的格式有问题