simple java solution, beats 94% of methods!


  • 1
    W
    public class Solution {
        public int getmax(int i,int[] n){
            int max=n[i+n[i]]; int res=i+n[i];
            for(int k=i+n[i];k>i;k--){
                if(max+res<n[k]+k){
                    max=n[k];
                    res=k;
                }
            }
            return res;
        }
        public int jump(int[] nums) {
            int ind=0; int c=1;
            if(nums.length==1)
                return 0;
            while(ind+nums[ind]<nums.length-1){
                ind=getmax(ind,nums);
                c++;
            }
            return c;
        }
    }
    

Log in to reply
 

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