[LeetCode] 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.)

这题是之前那道Jump Game 跳跃游戏 的延伸,那题是问能不能到达最后一个数字,而此题只让我们求到达最后一个位置的最少跳跃数,貌似是默认一定能到达最后位置的? 此题的核心方法还是利用动态规划Dynamic Programming的思想来解,我们需要两个变量cur和pre分别来保存当前的能到达的最远位置和之前能到达的最远位置,只要cur未达到最后一个位置则循环继续,pre记录cur的值,如果当前位置i小于等于pre,则更新cur然后i自增1。更新cur的方法是比较当前的cur和i + A[i]之中的较大值,等i循环到pre的值时,跳跃的步数加一,如果题目中未说明是否能到达末尾,我们还可以判断此时pre和cur是否相等,如果相等说明cur没有更新,即无法到达末尾位置,返回-1,代码如下:

解法一

class Solution {
public:
    int jump(int A[], int n) {
        int res = 0, i = 0, cur = 0;
        while (cur < n - 1) {
            int pre = cur;
            while (i <= pre) {
                cur = max(cur, i + A[i]);
                ++i;
            }
            ++res;
            if (pre == cur) return -1; // May not need this
        }
        return res;
    }
};

还有一种写法,跟上面那解法略有不同,但是本质的思想还是一样的,关于此解法的详细分析可参见网友实验室小纸贴校外版的博客,代码如下:

解法二

class Solution {
public:
    int jump(int A[], int n) {
        int res = 0, last = 0, cur = 0;
        for (int i = 0; i < n - 1; ++i) {
            if (i > last) {
                last = cur;
                ++res;
                if (cur >= n - 1) break;
            }
            cur = max(cur, i+A[i]);
        }
        return res;
    }
};
时间: 2024-10-05 04:27:36

[LeetCode] Jump Game II 跳跃游戏之二的相关文章

[LeetCode] 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

LeetCode: Jump Game II [044]

Perface 如果让你实现这个页面和一些操作的,比如点击1.2.3等就在那个input text中显示,还有删除功能,拨打我们先不要管它,只是模拟而已.要是我刚开始做的话,我会这样做: 用css.HTML布局那个界面 用javascript的事件委托监听那个按钮的父节点的点击事件 但是如果我想用面向对象的思想做呢?我是用Ext做的,所以我想说的是它帮我封装了很多.可能一些没用过Ext的人不太了解我下面贴的代码,但是我会尽量解释清楚的! Description ContactTelPanel =

[leetcode]Jump Game II @ Python

原题地址:https://oj.leetcode.com/problems/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 i

leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

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 nu

LeetCode: Jump Game II 解题报告

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 nu

[LeetCode] Flip Game 翻转游戏之二

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip twoconsecutive "++" into "--". The game ends when a person can no longer

【LeetCode每天一题】Jump Game II(跳跃游戏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.

[LeetCode] 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

[LeetCode]Jump Game II (贪心,维护当前最远能到达的位置和所需最少步数)

最少跳跃步数 第一想法是DP,复杂度O(n^2),但是对于大型数据会超时. Discuss中一种犀利的贪心方法,复杂度为O(n) class Solution { public: int jumpDP(int A[], int n) {//DP方法 int *dp=new int[n],j; memset(dp,127,sizeof(int)*n); dp[0]=0; int i=0; for(i=0;i<n-1;++i){ for(j=1;j<=A[i]&&i+j<n;