My Java solution, easy to understand


  • 0
    D

    The basic idea is that instead thinking about how many steps to get to the end, think about each step how much we can reach as much as possible.
    I hope it can help ;^)

    public class Solution {
        public int jump(int[] nums) {
            int reach = 0;
            int checkFrom = 0;
            int checkTo = -1;
            int steps = 0;
            while(reach < nums.length - 1){
                checkFrom = checkTo + 1;
                checkTo = reach;
                for(int i = checkFrom; i <= checkTo; i++){
                    reach = Math.max(reach, i + nums[i]);
                }
                steps++;
            }
            return steps;
        }
    }

Log in to reply
 

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