leetcode Q45

// 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& nums)
    {
        unordered_map> map;
        map[0] = pair(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(end+1,min(int(nums.size()-1),tmpEnd));
        }
    }
};

 

你可能感兴趣的:(leetCode,leetcode,动态规划)