Java Accepted Solution


  • 0
    D

    The idea is to calculate maximum reach for every index. If the maxReach couldn't move forward more than current index, we think we can't reach the end, can we? If maxReach reaches the end, it is done.

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

  • 0
    J

    can you explain your code?
    I think your code is simple and efficient!


  • 0
    D

    added the details on the question.


Log in to reply
 

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