9 lines Java solution


  • 0
    L
            if(nums==null||nums.length==0||k==0)	return new int[0];
            int[] ans = new int[nums.length-k+1];
            PriorityQueue<Integer> rec = new PriorityQueue<>((a, b)->(b-a));
            for(int i=0;i<nums.length;i++){
            	rec.offer(nums[i]);
            	if(i>=k)	rec.remove(nums[i-k]);
            	if(i>=k-1)	ans[i+1-k]=rec.peek();
            }
            return ans;
    

Log in to reply
 

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