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

SOLUTION 1:

参考:http://blog.csdn.net/fightforyourdream/article/details/14517453

我们可以使用贪心法来解决这个问题。

从后往前思考问题。以 des = len - 1反向思考。思考能到达它的最远的距离是多少。

例子:

2 3 1 1 4

i = 1

上例子中index i = 1是达到4的最远的距离。这时index i = 1 到4是最后一步的最优解,因为其它的解,如果跳到index = 2, 3 到达4为最后一步,那么倒数第二步既然可以到达 index = 2, 3 也可以到达index = 1,所以跳到index = 1步数会是一样的。所以其它的解最好的情况也就是与从index = 1跳过去一样而已。

而前面的点因为距离限制 ,有可能只能跳到index = 1,而不可以跳到index = 2, 3.所以 将倒数第二步设置在index = 1可以得到最多的解。

 package Algorithms.greedy;

 public class Jump {
public static void main(String[] strs) {
int[] A = {2, 3, 1, 1, 4};
System.out.println(jump(A));
} public static int jump(int[] A) {
if (A == null || A.length == 0) {
return 0;
} int len = A.length; int sum = 0; int des = len - 1;
while (des > 0) { // destination index
for (int i = 0; i < des; i++) { // 不断向前移动dest
if (A[i] + i >= des) { // 说明从i位置能1步到达dest的位置
sum++;
des = i; // 更新dest位置,下一步就是计算要几步能调到当前i的位置
//break; // 没必要再继续找,因为越早找到的i肯定越靠前,说明这一跳的距离越远
// 这一行可以去掉, des = i,不符合for的条件,会自动break.
System.out.println("sum:" + sum);
System.out.println("des:" + des);
}
}
} return sum;
}
}

SOLUTION 2:(2015.1.13 redo)

Leetcode增强test case之后,前面的算法不能通过,感谢http://fisherlei.blogspot.com/2012/12/leetcode-jump-ii.html的灵感:

[解题思路]
二指针问题,最大覆盖区间。
从左往右扫描,维护一个覆盖区间,每扫过一个元素,就重新计算覆盖区间的边界。比如,开始时区间[start, end], 遍历A数组的过程中,不断计算A[i]+i最大值(即从i坐标开始最大的覆盖坐标),并设置这个最大覆盖坐标为新的end边界。而新的start边界则为原end+1。不断循环,直到end> n.

 // solution 2: one pass greedy.
public int jump(int[] A) {
if (A == null || A.length == ) {
return ;
} // bug:
/*
Input: [1]
Output: 1
Expected: 0
*/
if (A.length == ) {
return ;
} int len = A.length; int start = ;
int end = ; int steps = ;
while (end < len - ) {
int max = ;
steps++;
for (int i = start; i <= end; i++) {
max = Math.max(max, i + A[i]); if (max >= len - ) {
return steps;
}
} start = end + ;
end = max; if (start > end) {
break;
}
} return Integer.MAX_VALUE;
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/greedy/Jump.java

LeetCode: Jump Game II 解题报告的更多相关文章

  1. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  2. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  3. 【LeetCode】45. Jump Game II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  4. 【原创】leetCodeOj --- Jump Game II 解题报告

    原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...

  5. LeetCode: Jump Game Total 解题报告

    Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...

  6. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  7. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  8. LeetCode: Path Sum II 解题报告

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  9. LeetCode: Palindrome Partitioning II 解题报告

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

随机推荐

  1. java restful接口

    用json-lib的jar包输出json串: public void responseJason(HttpServletResponse response, Object obj){ ObjectMa ...

  2. 【MySQL】MySQL的常规操作

    MySQL的常规知识 标准的SQL语句通常可分为如下的几种类型: 1,DCL(Database Control Language) :数据控制语言,主要由grant和revoke关键字组成. 2.DD ...

  3. 【Algorithm】自底向上的归并排序

    一. 算法描述 自底向上的归并排序:归并排序主要是完成将若干个有序子序列合并成一个完整的有序子序列:自底向上的排序是归并排序的一种实现方式,将一个无序的N长数组切个成N个有序子序列,然后再两两合并,然 ...

  4. 日志收集之--将Kafka数据导入elasticsearch

    最近需要搭建一套日志监控平台,结合系统本身的特性总结一句话也就是:需要将Kafka中的数据导入到elasticsearch中.那么如何将Kafka中的数据导入到elasticsearch中去呢,总结起 ...

  5. EasyUI datagird 排序 按数字类型的问题

    easyui datagird 默认显示的数据都是字符, 对要数字列进行排序规则,需要自定义排序规则如果按字符排序 27竟然小于4 这不是我们想要的.解决方案 <table id='grid'c ...

  6. 一步一步掌握java的线程机制(一)----创建线程

    现在将1年前写的有关线程的文章再重新看了一遍,发现过去的自己还是照本宣科,毕竟是刚学java的人,就想将java的精髓之一---线程进制掌握到手,还是有点难度.等到自己已经是编程一年级生了,还是无法将 ...

  7. Android Studio:正确导入SO相关文件

      导入so文件有2种方式 第一种: 使用jniLibs文件夹导入so文件,则仅需将所有cpu类型的文件夹拷进去. 在project结构下,module目录下创建libs文件夹,放入jar文件:在sr ...

  8. cucumber java从入门到精通(2)用代码定义步骤

    cucumber java从入门到精通(2)用代码定义步骤 上一节里我们定义了feature文件,feature文件就是自然语言描述的用例文件,它有一定的章法,具体的潜规则是: 使用Feature关键 ...

  9. [转]IDEA 导出自己的jar包 并且在另一个工程中引用

    1.导出jar包 1.1 idea导出jar包不如eclipse方便,但是熟练了也很容易操作 1.2 File -> Project Settings -> Artifacts(艺术品) ...

  10. JavaScript语言精粹--replace()与正则

    今天有人问我repalce(),他那个题目很有意思.我也不会做,于是我就去查,结果发现就是最基础的知识的延伸. 所以啊最基础的知识才是很重要的,千万不能忽略,抓起JS就写代码完全不知到所以然,只知道写 ...