Java O(N^2) simple solution


  • 0
    I
    public class Solution {
        public int jump(int[] nums) {
            
            int[] jumps = new int[nums.length];
            
            for(int i = 0; i < nums.length - 1; i++) {
                if(i > 0 && nums[i] + 1 == nums[i - 1]) {
                    continue;
                }
                
                int j = i + 1;
                int steps = 1;
                while(j < nums.length && steps <= nums[i]) {
                    
                    if(jumps[j] == 0 || jumps[j] > jumps[i] + 1) {
                        jumps[j] = jumps[i] + 1;
                    }
                    
                    steps++;
                    j++;
                }
            }
            
            return jumps[jumps.length - 1];
        }
    }
    

  • 1
    S

    My O(n^2) solution gave me a TLE !


  • 0
    A

    @shilpa6 mine too


Log in to reply
 

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