Why my code exceeds the time,can anyone help?


  • 0
    M

    public class Solution {
    public int jump(int[] nums) {
    int start = 0,count = 0;
    int max_step = 0;
    int step = 0;
    if(nums.length == 0 ||nums.length == 1)
    return 0;
    while(nums[start]+start<(nums.length-1)){
    int i;
    if(nums[start]==0)
    return -1;
    max_step = 0;
    for(i=1;i<=nums[start];i++){
    max_step = Math.max(max_step, i+nums[start+i]);
    step = i;
    }
    start +=step;
    count++;
    }
    if(start>=nums.length-1){
    return count;
    }
    else{
    count++;
    }
    return count;
    }
    }


Log in to reply
 

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