【LeetCode】Jump Game & Jump Game II

这篇博客介绍了LeetCode上的两道题目——Jump Game和Jump Game II,涉及非负整数数组中跳跃到达最后一个元素的可能性和最小跳跃次数。提供了题目描述和分析,并推荐了C++的学习资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考链接

http://blog.youkuaiyun.com/pickless/article/details/9855891
http://www.cnblogs.com/tgkx1054/archive/2013/05/23/3095115.html

http://www.cnblogs.com/xinsheng/p/3510504.html

题目描述

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.



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
这是一个经典的贪心算法。
我们只关心,最远可以到达哪里。因为能够到达最远的地方了,那么最远之前的地方必然可以到达。
代码中的几个要点max = A[i] + i > max ? A[i] + i : max;如果从i点可以跳更远那么就更新最远点。
i <= max 这个条件必须满足,否则i都到达不了,后面的就更没办法到达了。


Jump Game
class Solution {
public:
    bool canJump(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int max = 0;
        for (int i = 0; i < n && i <= max; i++) {
            max = A[i] + i > max ? A[i] + i : max;
        }
        return max >= n - 1;
    }
};
Jump Game II

思路:
声明一个数step[n],step[i]表示到i所用的最小步数。
class Solution { 
public: 
    int jump(int A[], int n)  
    { 
        int *step=new int[n];
        memset(step,0,sizeof(step));
        int lasti=0,maxreach=A[0],reachindex;  
        for(int i=1;i<n;i++) 
        { 
           if(lasti+A[lasti]>=i)
           {
              step[i]=step[lasti]+1;
           }
           else
           {
               step[i]=step[reachindex]+1;
               lasti=reachindex;
           }
           if(i+A[i]>maxreach) 
           { 
              maxreach=i+A[i]; 
              reachindex=i;             
           } 
        } 
        return step[n-1];        
    } 
}; 


分析:
假设第i步可以跳的范围是start,end
那第i+1步能跳进的范围是end+1到(从start到end所能跳跃的最远距离)
这样每一步都用贪心算法,可以得出最小步数。

class Solution {
public:
    int jump(int A[], int n) {
        if(n <= 1)
            return 0;
 
        int start = 0, end = 0;
        int cnt = 0;
        while(end < n-1) {
            int maxdis = 0;
            for(int i = start; i <= end; i ++) {
                maxdis = max(maxdis, A[i]+i);
            }
            start = end+1;
            end = maxdis;
            cnt ++;
        }
        return cnt;
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值