leetcode Q45

该博客介绍了一种使用动态规划解决跳跃游戏的方法。在给定整数数组nums中,每个元素表示可以跳跃的最大步数,目标是找到达到数组最后一个元素所需的最小步骤数。博主通过一个名为`Solution`的类实现了一个`jump`函数,利用unordered_map存储到达每个位置的最小步数,并逐步更新状态,直到找到目标位置。
摘要由CSDN通过智能技术生成
// the point of solving this kind of problem is that we should realize that
// their DP array(store minimum steps) must be like [0,1*,2*,3*,''',N*]
// (here * means one or more)

class Solution {
public:
    int jump(vector<int>& nums)
    {
        unordered_map<int,pair<int,int>> map;
        map[0] = pair<int,int>(0,0);
        int step = 0;
        while(true)
        {
            int start = map[step].first;
            int end = map[step].second;
            if(end==nums.size()-1)
            {
                return step;
            }
            int tmpEnd = -1;
            for(int i = start;i<=end;i++)
            {
                tmpEnd = max(tmpEnd,nums[i]+i);
            }
            map[++step] = pair<int,int>(end+1,min(int(nums.size()-1),tmpEnd));
        }
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值