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.

  For example:
  A = [2,3,1,1,4], return true.

  A = [3,2,1,0,4], return false.

  从题目的意思是给一个非负的整数数组,你的初始位置在第一个元素,每个元素的值代表该位置可以跳跃的最大距离。用算法判断你是否可以到达最后一个元素。

  提示的标签是数组和贪心(greedy),但是这个应该是动态规划解,因为贪心算法需要保证必须有解,这里尚需判断,并不能保证。

  我们用maxposition维护一个从开始位置能到达的最远位置,然后判断在当前位置是否能够到底最后一个位置和当前位置是否可达,如果两个条件都满足,那么返回true,如果当前位置是0,并且最远位置不能超过当前位置,那么只能返回false 了,更新最远位置。java代码如下:

    public boolean canJump(int[] A){
if(A.length <= 1)
return true;
if(A[0] >= (A.length-1))
return true;
int maxposition = A[0];
if(maxposition == 0)
return false;
for(int i = 1; i < A.length - 1; i++){
if(maxposition >= i && (i + A[i]) >= A.length -1)
return true;
if(maxposition <= i && A[i] == 0)
return false;
if(maxposition < (i + A[i]))
maxposition = i + A[i];
}
return false;
}

或者可以这样写

    public boolean canJump(int[] A){
int maxposition = 0;
for(int start = 0; start <= maxposition && start < A.length; start ++){
if((A[start] + start) > maxposition)
maxposition = (A[start] + start);
if(maxposition >= (A.length - 1))return true;
}
return false;
}

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.

  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.)

  题目的意思是求到达最后元素的最小跳跃步数。用贪心算法,解法如下(java),可以通过。但是如果没有解呢?或者贪心法找不到解呢?

    public int jump(int[] A){
int maxx=0,temp=0,count=0;
for(int i = 0; i < A.length;){
if(temp >= (A.length-1)) break;
while(i <= temp)
{
maxx = maxx>(i + A[i])?maxx:(i + A[i]);
i++;
}
count++;
temp = maxx;
}
return count;
}

[leetcode解题记录]Jump Game和Jump Game II的更多相关文章

  1. LeetCode解题记录(贪心算法)(二)

    1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...

  2. Leetcode解题记录

    尽量抽空刷LeetCode,持续更新 刷题记录在github上面,https://github.com/Zering/LeetCode 2016-09-05 300. Longest Increasi ...

  3. LeetCode解题记录(贪心算法)(一)

    1. 前言 目前得到一本不错的算法书籍,页数不多,挺符合我的需要,于是正好借这个机会来好好的系统的刷一下算法题,一来呢,是可以给部分同学提供解题思路,和一些自己的思考,二来呢,我也可以在需要复习的时候 ...

  4. LeetCode解题记录(双指针专题)

    1. 算法解释 双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务.也可以延伸到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,则也称为滑动窗口(两个指针包围的区域 ...

  5. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  6. Leetcode解题思想总结篇:双指针

    Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...

  7. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  8. pwnable.kr input解题记录

    pwnable input解题记录 给了源码如下: #include "stdio.h" #include "unistd.h" #include " ...

  9. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. HDU 4283 区间DP You Are the One

    题解 我使用记忆化搜索写的.

  2. go 和make的用法 区别

    Doand Make are two verbs which frequently confuse students of English. Learn the Difference between ...

  3. Python中字典的key都可以是什么

    作者:Inotime 来源:CSDN 原文:https://blog.csdn.net/lnotime/article/details/81192207 答:一个对象能不能作为字典的key,就取决于其 ...

  4. 【LeetCode】String Without AAA or BBB(不含 AAA 或 BBB 的字符串)

    这道题是LeetCode里的第984道题. 题目要求: 给定两个整数 A 和 B,返回任意字符串 S,要求满足: S 的长度为 A + B,且正好包含 A 个 'a' 字母与 B 个 'b' 字母: ...

  5. HDU-5536 Chip Factory,又见字典树,好题+1!

    Chip Factory 题意:一个n个数的数列,求三个数其中两个数的和与另外一个数的异或值最大,输出这个最大值. 思路:和前面那个百度之星资格赛HDU4825的类似,多了两个过程,一个是枚举和,另一 ...

  6. Git升级迁移

    1 原有机器进行升级和备份: https://about.gitlab.com/update/#centos-6 1. Make a backup (Optional) If you would li ...

  7. JSF框架整理

    JSP体系结构: JSF主要优势之一就是它既是Java web 应用程序的用户界面标准又是严格遵循 模型-视图-控制器(MVC)设计模式的框架. 用户界面代码(视图)和应用程序数据和逻辑(模型)的清晰 ...

  8. BZOJ 2134 单选错位 ——期望DP

    发现概率是∑1/两道题答案相同的概率, 稍加化简 #include <map> #include <ctime> #include <cmath> #include ...

  9. properties类的基本使用方法

    properties类的基本使用方法1.假设有“pp.properties”,内容有       age=22       2.java中用下面方法:   Properties   props   = ...

  10. selenium 与 firefox版本不兼容报错

    org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 ...