暴力方法:超时
class Solution {public int[] maxSlidingWindow(int[] nums, int k) {int n=nums.length;int size=n-(k-1);int[] res=new int[size];int slow=0,quick=k-1;int maxNum=findMax(nums,slow,quick);res[0]=maxNum;while (quick<n-1){quick++;int addNum=nums[quick];int removeNum=nums[slow];if(addNum>maxNum){maxNum=addNum;}if(removeNum==maxNum){maxNum=findMax(nums,slow+1,quick+1);}slow++;res[slow]=maxNum;}return res;}public int findMax(int[] nums, int slow, int quick){int maxNum=nums[slow];for(int i=slow;i<=quick;i++){maxNum=Math.max(maxNum,nums[i]);}return maxNum;}
}
PriorityQueue:超时
public int[] maxSlidingWindow(int[] nums, int k) {int n=nums.length;int size=n-(k-1);PriorityQueue<Integer> heap = new PriorityQueue<>((v1,v2)->(v2-v1));int slow=0,quick=k-1;for(int i=slow;i<=quick;i++){heap.offer(nums[i]);}int[] res=new int[size];res[0] = heap.peek();while (quick<n-1){quick++;heap.offer(nums[quick]);heap.remove(nums[slow]);slow++;res[slow]=heap.peek();}return res;}