Java concise O(n) Solution


  • 0

    Begin with index 0 and terminate when we reach last index.
    Save max index we can reach.
    If index i go faster than max index we know it's wrong.


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

Log in to reply
 

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