My simple, easy understanding Java Solution


  • 0
    X
     public int jump(int[] A) {
        int len=A.length;
        int result=0;
        int count=0;
        while (result<len-1) {
            count++;
            if (A[result]+result>=len-1) {
                return count;
            }
            result=maxjump(A,result);
        }
            return count;
    }
    
    public int maxjump(int[] A, int num){
    int len=A.length;
    int max=0;
    int index=0;
    if (A[num]+num>len-1) {
        return A[num]+num;
    }
    for (int i=num+1;i<=(num+A[num]) ;i++ ) {
        if ((A[i]+i)>max) {
            max=A[i]+i;
            index=i;   
        }
        // System.out.println(num+"  "+max);
    }
    return index;
    

    }


Log in to reply
 

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