Two different solutions both accepted as best in C


  • 0
    bool canJump(int* nums, int size)
    {
        if(size < 2) return true;
        for(int cur=size-2; cur > -1; cur--)
        {
            if(nums[cur] == 0)
            {
                int len = 1;
                while(len > nums[cur])
                {
                    len++;
                    cur--;
                    if(cur < 0) return false;
                }
            }
        }
        return true;
    }
    

    bool canJump(int* nums, int size)
    {
        int max = 0;
        for(int i = 0; i < size; i++, max--)
        {
            max = max > nums[i]? max : nums[i];
            if(max<1 && i!=size-1) return false;
        }
        return true;
    }

Log in to reply
 

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