8line 4ms easy understand solution using c++


  • 0
    C

    The key is

    step = max(--step, nums[i]);
    

    For each nums[i], we can figure out how far it could be from this point, then we can easily solve it.

    class Solution {
    public:
        bool canJump(vector<int>& nums) {
            if(nums.size()==1) return true;
            if(nums[0]==0) return false;
            int step = nums[0];
            for(int i=1; i<nums.size()-1; i++){
                step = max(--step, nums[i]);
                if(step<=0) return false;
            }
            return true;
        }
    };

Log in to reply
 

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