Could anybody help me to modify this code? It always receives a TLE.


  • 0
    T
    public int jump(int[] nums) {
        if(nums.length <= 1){
            return 0;
        }
        int maxIndex = 0;
        int[] jumpCounts = new int[nums.length];
        jumpCounts[0] = 0;
        
        for(int i = 0; i < nums.length - 1; i++){
            int j = i + 1;
            maxIndex = Math.max(i + nums[i], maxIndex);
            
            while(j < nums.length && j <= maxIndex){
                if(jumpCounts[j] == 0){
                    jumpCounts[j] = jumpCounts[i] + 1;
                }
                j++;
            }
        }
        
        return jumpCounts[nums.length - 1];
    }

Log in to reply
 

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