【LeetCode】 Jump Game 系列

【LeetCode】 Jump Game 系列

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

解答

两种方法:
1. 动态规划:
record[i]代表下表为i的元素能否到达末尾元素。
按照自底而上的规则来填充数组元素,然后返回record[0]。
时间复杂度较高,且需要额外空间存储。而且实际中很可能因为超出时间限制。

class Solution {
public:   
  //动态规划方法:时间复杂度较高
    bool canJump(vector<int>& nums) 
    {
        vector<bool> record(nums.size(),false);
        initRecord(nums,record);
        return record[0];
    }
private:    
    void initRecord(const vector<int> &nums,vector<bool> & record)
    {
        int size = nums.size();
        int n = size-1;
        record[n] = true;
        while(--n >= 0)
        {
            int gap = nums[n];
            for(int i = 1; i <= gap && n+i < size; ++i)
            {
                if(record[n+i]) 
                {
                    record[n] = true;
                    break;
                }
            }
        }
    }
};
  1. 贪心算法
    这个算法也是LeetCode上的高票答案,算法十分精简有效,线性时间并且无须额外存储空间。
    从数组首元素开始计算我们最终能够到达的元素位置的最大值,如果此值大于等于num.size()-1。表示我们能够到达末尾元素,否则则不可以。
class Solution {
public:
    bool canJump(vector<int>& nums) 
    {
        int n = nums.size();
        int reach = 0;
        for(int i = 0; i < n && i <= reach; ++i)
        {
            reach = max(i+nums[i],reach);
        }
        return (reach >= n-1);
    }  
};

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

解答

考虑使用BFS

我们可以使用BFS算法来解决这个问题。

使用两个指针,start和end,表示着一个范围,我们在这个范围内寻找能够到达的最远位置maxEnd。

所以我们可以确定通过一次jump,我们可以到达[end+1,maxEnd]。然后我们增加一次step,并且让start = end+1,end = maxEnd。重新开始新一轮的寻找maxEnd。

我们不断的更新start和end的值,直到end能够等于或大于最后一个位置。

class Solution {
public:
    int jump(vector<int> &nums)
    {
        int start = 0, end = 0, step = 0;
        int n = nums.size()-1;
        while(end < n)
        {
            int maxEnd = end;
            while(start <= end)
            {
                maxEnd = max(maxEnd,start+nums[start]);
                if(maxEnd >= n) return step+1;
                ++start;
            }
            end = maxEnd;
            ++step;
        }
        return step;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值