c++ 12 ms DP-ish


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.