C++, elegant solution, O(n) time.


  • 18
    1
    bool canJump(vector<int>& nums) {
        int size=nums.size();
        int step=nums[0];
        for(int i=1;i<size;++i){
            step--;
            if(step<0)
               return false;
            if(nums[i]>step)
               step=nums[i];
        }
        return true;
    }

  • -4
    F

    Cool!! thanks! though it looks like a C solution. I would like to shrink it to:

    bool canJump(int *nums, int numSize)
    {
        for(int i=1, step=nums[0];i<numsSize;++i){
            step--;
            if(step < 0)
               return false;
            if(nums[i] > step)
               step = nums[i];
        }
        return true;
    }

Log in to reply
 

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