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. django 常见过滤器

      一.形式:小写 {{ name | lower }} 二.过滤器是可以嵌套的,字符串经过三个过滤器,第一个过滤器转换为小写,第二个过滤器输出首字母,第三个过滤器将首字母转换成大写 标签 {{ st ...

  2. asp.net下js调用session

    大致方法为:js调用webservise,然后通过webservise将session值返回给js完成调用 其实最主要的一点就是在webmethod中允许session:[WebMethod(Enab ...

  3. hdu 1527 威佐夫博弈

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. PAT天梯赛练习题——L3-004. 肿瘤诊断(三维连通块并查集)

    L3-004. 肿瘤诊断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶 ...

  5. [USACO5.3]Big Barn (动态规划)

    题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N x N 的方格.输入数据中包括有树的 ...

  6. Spring Boot SpringSecurity5 身份验证

    对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache Shiro.Spring Security). pom.xm ...

  7. 酒厂选址(codevs 1507)

    题目描述 Description Abstinence(戒酒)岛的居民们酷爱一种无酒精啤酒.以前这种啤酒都是从波兰进口,但今年居民们想建一个自己的啤酒厂.岛上所有的城市都坐落在海边,并且由一条沿海岸线 ...

  8. EC++学习笔记(五) 实现

    条款26:尽可能延后变量定义式的出现时间 尽可能延后变量的定义,知道非得使用该变量的前一刻为止方法A: Widget W; ; i < n; ++i) { W = ... } 方法B: ; i ...

  9. java 汉字保存到mysql 乱码

    保存之前正常,插入数据乱码 确认jsp mysql编码都确定为utf8 在连接数据库是加上编码 jdbc:mysql://localhost:3306/test?useUnicode=true& ...

  10. TYVJ3680 找妹子

    时间: 1000ms / 空间: 1200KiB / Java类名: Main 背景 本题由 @fjzzq2002 提供,已奖励20金币. 描述 sps是zzq的好伙伴. sps一天叫来了许多个妹子. ...