Sharing my solution, java, O(1), considering [0, 0] case


  • 1
    B
    public class Solution {
        public int jump(int[] array) {
            if(array == null || array.length <= 1) return 0;
            int oldMax = array[0];
            int newMax = array[0];
            int ret = 1;
            for(int i = 1; i < array.length; i++) {
                if(oldMax >= array.length - 1) return ret;
                if(i > oldMax) {
                    ret++;
                    oldMax = newMax;
                    if(i > oldMax) return Integer.MAX_VALUE;
                }
                newMax = Math.max(newMax, i + array[i]);
            }
            return ret;
        }
    }

Log in to reply
 

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