Solution working on my machine. Not working on OJ


  • 0
    S

    The below solution works great on my computer. Returns compiler error in oj

    import java.util.ArrayList;
    import java.util.LinkedList;
    class Solution {
        public int jump(int[] nums) {
            boolean[] marked = new boolean[nums.length];
            int[] pathLengthTo = new int[nums.length];
            Arrays.fill(pathLengthTo,Integer.MAX_VALUE);
            LinkedList<Integer> queue = new LinkedList<Integer>();
            queue.offer(0);
            marked[0]=true;
            pathLengthTo[0]=0;
            while(queue.peek()!=null)
            {
                int currentIndex = queue.poll();
                for(int i=1;((currentIndex+i)<nums.length)&&(i<=nums[currentIndex]);i++)
                {
                    
                    if(!marked[currentIndex+i])
                    {
                        queue.offer(currentIndex+i);
                        marked[currentIndex+i]=true;
                    }
                    if((pathLengthTo[currentIndex]+1)<pathLengthTo[currentIndex+i])
                    {
                        pathLengthTo[currentIndex+i]=pathLengthTo[currentIndex]+1;
                    }
                }
            }
            return pathLengthTo[nums.length-1];
            
        }
    }

Log in to reply
 

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