Getting runtimeexception


  • 0
    L

    This code runs fine on an IDE but I am getting the RuntimeException while running it on LeetCode. Does anyone know why?

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.PriorityQueue;
    
    public class Solution
    {
      public int[] maxSlidingWindow(int[] nums, int k)
      {
        int i = 0;
        final PriorityQueue<wrapper> maxHeap = new PriorityQueue<>(k);
        final List<Integer> list = new ArrayList<Integer>();
        // Get first k elements.
        if (k > nums.length)
        {
          return list.stream().mapToInt(e -> e).toArray();
        }
        // add first k elements.
        addToHeap(maxHeap, nums, 0, k);
        list.add(maxHeap.peek().value);
        i = k;
        while (i < nums.length)
        {
          maxHeap.remove(new wrapper(i - k, nums[i - k]));
          addToHeap(maxHeap, nums, i, i + 1);
          list.add(maxHeap.peek().value);
          i++;
        }
        return list.stream().mapToInt(e -> e).toArray();
      }
    
    
    
      private void addToHeap(PriorityQueue<wrapper> maxHeap, int[] nums,
          int i, int k)
      {
        for (int index = i; index < k; index++)
        {
          maxHeap.add(new wrapper(index, nums[index]));
        }
      }
    
      static class wrapper implements Comparable<wrapper>
      {
        int index;
    
        int value;
    
    
    
        wrapper(int index, int value)
        {
          this.index = index;
          this.value = value;
        }
    
    
    
        @Override
        public boolean equals(Object o)
        {
          final wrapper w = (wrapper) o;
          return w.index == index;
        }
    
    
    
        @Override
        public int compareTo(wrapper w)
        {
          return w.value - value;
        }
      }
    }
    

Log in to reply
 

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