57. Jump Game && Jump Game II

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.

思路:若能走的最远距离小于当前距离,说明走不过来。否则,利用此位置情况更新最远距离。

class Solution {
public:
    bool canJump(int A[], int n) {
        int reached = 0;
        for(int i = 0; i < n; ++i) {
            if(reached < i) return false;
            if(i+A[i] > reached) reached = i+A[i];
        }
        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.

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

思路:

方法1: 动态规划。(能走到下标为 n-1 位置时,就结束。)

class Solution {
public:
    int jump(int A[], int n) {
        if(n == 0) return 0;
        vector<int> step(n, INT_MAX);
        step[0] = 0;
        int reached = 0;
        for(int i = 0; i < n-1; ++i) {
            if(reached < i || step[i] >= step[n-1]) break;
            if(i + A[i] > reached) {
                reached = i+A[i];
                for(int j = i+1; j < n && j <= reached; ++j)
                    step[j] = min(step[j], step[i]+1);
            }
        }
        return step[n-1];
    }
};

方法二 : 从前往后跳,每一步长内选择选择能跳到下一步长最远的点。 第一个步长为 0 - A[0], 第二步长为 A[0] - max(0+A[0],..., A[0] + A[A[0]]),

从 0->A[0]->maxA[i](0+A[0],...,A[i] + A[A[i]], ... , A[0] + A[A[0]]);

class Solution {
public:
    int jump(int A[], int n) {
        int last = 0, step = 0, maxV = 0;
        for(int i = 0; i < n; ++i) {
            if(i > last) {
                last = maxV;
                ++step;
            }
            maxV = max(maxV, i + A[i]);
        }
        return step;
    }
};

方法三: 从后往前确定应该走的位置。(从前往后遍历,如果 i + A[i] >= last , 说明其第一个能走到最后, 更新 last = i).

class Solution {
public:
    int jump(int A[], int n) {
        int last = n-1, step = 0;
        while(last > 0) {
            for(int i = 0; i < last; ++i) {
                if(i+A[i] >= last) {
                    last = i;
                    step++;
                    break;
                }
                else if(i == last) return INT_MAX;
            }
        }
        return step;
    }
};

附加:以下这个代码也可以 AC. (但是常识上不觉得是对的)

class Solution {
public:
    int jump(int A[], int n) {
        int last = n-1, step = 0;
        while(last > 0) {
            for(int i = 0; i < last; ++i) {
                if(i+A[i] >= last) {
                    last = i;
                    step++;
                }
            }
        }
        return step;
    }
};
时间: 2024-08-01 22:31:21

57. Jump Game && Jump Game II的相关文章

[LeetCode#55, 45]Jump Game, Jump Game II

The problem: 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

hdu 5162 Jump and Jump...(水题)

Problem Description There are n kids and they want to know who can jump the farthest. For each kid, he can jump three times and the distance he jumps is maximum distance amount all the three jump. For example, if the distance of each jump is (10, 30,

BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 kid 的最终成绩.然后根据每个 kid 的最终成绩,对他们进行排名. 赛中写得比较丑陋,这个还比较顺眼....呵呵 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <

【HDU】5162 Jump and Jump...

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5162 发现自己写的max宏有点问题 改成这样:#define max(a,b) ((a>b)?(a):(b)),后面的返回值也要一个括号 #include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <string> #include

杭电oj 1087——super jump!jump!jump(java实现)

question:Super Jumping! Jumping! Jumping! 意思就是找一串数字中的和最大子串 思路:创建另一个数组,每一项是路径数组对应项之前最大子串的和,然后遍历此数组找出最大值即可(也是参考了别人的博客,下面是链接,这是接触的第一道dp题,希望慢慢的自己也会写!) 原文链接 source code: package hduoj; import java.util.Scanner; public class hdoj_1087 { public static void

[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 exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

TC SRM 633div1

250pts   PeriodicJumping 题意:从起点开始,每次按找数组jump给定的长度,即jump[0], jump[1], jump[2].....jump[n-1], 向各个方向跳,跳完后从从头开始,问最后能否达到(x,0). 限制:|x| <= (int)1e9,n <= 50, 1 <= len[i] <= (int)1e9. 分析: 题解分析的很详细,每次跳完后可达的范围离原点的距离总是一个区间[a,b],考虑下一次跳的长度r之后,可达的范围, (1). r

【Peaks加强版 BZOJ 3551】你被坑了吗?

这道在没加读入优化时间在20s左右的题终于在大米饼两天的死缠烂打.鬼混.乱整乱撞后艰难地AC了.但惋惜的是,大米饼一号代码其实更加简洁,但至今找不出BUG,我将它放在下面,也许有一天从远方来的另一个大米饼会拯救它.让我们一起念出这道题的关键字: Kurskal,LCA倍增,Kurskal重构树,dfs序,主席树. 比较新鲜的是重构树,它有一些美妙性质. 红色的是由边化成的节点.用Kurskal建出这棵树,在这一棵树上进行DFS序处理,并且在DFS序上进行主席树(可持久化线段树)处理.有趣的是,这

英语电影剧本大全(中英对照)

目     录 <泰坦尼克号>全部英文剧本 TV REPORTER: Treasure hunter Brock Lovett is best known for finding Spanish gold off islands in the best Caribbean. LIZZY: It’s OK, I’ll get you in a minutes. Come on. TV REPORTER: Now he is using Russian subs to reach the most