Skip to content

45. Jump Game II

Linjie Pan edited this page Apr 8, 2019 · 1 revision

We use far to record current furthest place we can reach. Traverse the array to update far, each time current place reach the furthest place the step plus one. Keep traversing until far reach the last place of array.

    public int jump(int[] nums) {
        int far = 0, result = 0, i = 0;
        while( i < nums.length && far < nums.length - 1 ) {
            int max = 0;
            while( i <= far ) {
                max = Math.max(max, nums[i] + i);
                i++;
            }
            far = max;
            result++;
        }
        return result;
    }
Clone this wiki locally