3ms java solution


  • 0
    B
    public class Solution {
        public boolean canJump(int[] nums) {
            int s=1;
            for(int i=0; i <nums.length-1; i++){
                if(s > 0){
                    s = Math.max(s-1,nums[i]);
                }
            }
            if(s > 0) return true;
            return false;
        }
    }

  • 0
    A

    Concise O(n) python sulution without extra space

    def canJump(self, nums):
        reach = 0
        reachedEnd = False
        for i, num in enumerate(nums):
            if i < reach:
                break
            reach = max(reach, i+num)
            if reach >= len(nums)-1:
                reachedEnd = True
                break
        return reachedEnd

Log in to reply
 

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