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. struts 乱码

    在进行struts开发的过程中,总也是出现很多的乱码问题 ,但归根到底,也只是以下三种情况: ㈠页面显示中文乱码 ㈡传递参数中文乱码 ㈢国际化资源文件乱码 下面就这三中情况介绍怎么在具体项目中处理这些 ...

  2. 二丶人生苦短,我用python【第二篇】

    1 编码 python解释器在加载 .py 文件中的代码时,对内容默认进行ascill编码,因此存在中文会报错,所以需要告诉python解释器,用什么编码来执行源代码.) ASCII码,主要用于显示现 ...

  3. Python (Page Object实例)

    Page Object是Selenium自动化测试项目开发实践的最佳设计模式之一,通过对界面元素和功能模块的封装减少冗余代码,同时在后期维护中,若元素定位或功能模块发生变化,只需要调整页面元素或功能模 ...

  4. 微软.net一些类的源码

    地址:http://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs 关键字:refer ...

  5. OkHttpUtil

    package jp.co.gunmabank.util import android.os.Handlerimport android.os.Looperimport com.google.gson ...

  6. 【组合数+Lucas定理模板】HDU 3037 Saving

    acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...

  7. 转载:CreateMutex WaitForSingleObject ReleaseMutex使用

    HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes,// BOOL bInitialOwner,  // flag for init ...

  8. Virtual Box 安装过程(卸载Vmware后)

    VirtualBox安装前的操作:(或许某些操作不一定有用,但是我是这么做下来的,最后也安装成功了) 步骤一:停止之前安装的vmware的所有服务(如果之前没有安装过虚拟机软件,无需做此操作)VMwa ...

  9. vue elementUI 表单校验(数组多层嵌套)

    在使用vue element-ui form表单渲染的时候,会遇到这样的数据结构: { "title":''123455, "email":'123456@qq ...

  10. 虚拟机vmnet0、vmnet1和vmnet8的区别 虚拟网卡概述

    vmnet0,实际上就是一个虚拟的网桥 vmnet0,实际上就是一个虚拟的网桥,这个网桥有很若干个端口,一个端口用于连接你的Host,一个端口用于连接你的虚拟机,他们的位置是对等的,谁也不是谁的网关. ...