【Jump Game II 】cpp

本文详细解析了一种算法问题——如何在给定数组中找到从第一个元素到达最后一个元素所需的最小跳跃次数。通过贪心策略,文章给出了清晰的实现步骤及代码示例。

题目:

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

代码:

class Solution {
public:
    int jump(vector<int>& nums) {
            int max_jump=0, next_max_jump=0, min_step=0;
            for ( int i = 0; i<=max_jump; ++i )
            {
                if ( max_jump>=nums.size()-1 ) break;
                if ( max_jump < i+nums[i] ) next_max_jump = std::max(next_max_jump, i+nums[i]);
                if ( i==max_jump )
                {
                    max_jump = next_max_jump;
                    min_step++;
                }
            }
            return min_step;
    }
};

tips:

参考Jump Game的Greedy思路。

这道题要求求出所有可能到达路径中的最短步长,为了保持O(n)的解法继续用Greedy。

这道题的核心在于贪心维护两个变量:

max_jump:记录上一次跳跃能跳到最大的下标位置

next_max_jump:记录遍历所有max_jump之前的元素后,下一次可能跳到的最大下标位置

举例说明如下:

原始数组如右边所示:{7,0,9,6,9,6,1,7,9,0,1,2,9,0,3}

初始:max_jump=0 next_max_jump=0 min_step=1

i=0:next_max_jump=7  更新max_jump=7 更新min_step=1

i=1: 各个值不变

i=2: i+nums[i]=2+9=11>7 更新next_max_jump=11

i=3:i+nums[i]=3+6=9<11 不做更新

i=4: i+nums[i]=4+9=13>11 更新max_jump=13

...

i=7:i+nums[i]=7+7=14 >= nums.size()-1 返回min_step=2

完毕

==========================================

第二次过这道题,不太顺,大体复习了下思路。

class Solution {
public:
    int jump(vector<int>& nums) {
            if ( nums.size()==0 ) return 0;
            int ret = 0;
            int nextJump = 0;
            int maxLength = 0;
            for ( int i=0; i<=maxLength; ++i )
            {
                if ( maxLength>=nums.size()-1 ) break;
                nextJump = std::max(i+nums[i], nextJump);
                if ( i==maxLength )
                {
                    maxLength = nextJump;
                    ret++;
                }
            }
            return ret;
    }
};

==========================================

第三次过这道题,把代码改了一行,但是整体结构清晰了不少。

class Solution {
public:
    int jump(vector<int>& nums) {
        if (nums.size()<2) return 0;
        int steps = 0;
        int local = 0;
        int next = local;
        for ( int i=0; i<=local; ++i )
        {
            next = max(next, i+nums[i]);
            if ( i==local )
            {
                steps++;
                local = next;
                if ( local>=nums.size()-1 ) return steps;
            }
        }
        return 0;
    }
};

next只负责看下一跳能够到哪。

什么时候更新local了,再判断能否跳到尾部。

转载于:https://www.cnblogs.com/xbf9xbf/p/4539456.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值