classSolution{publicint[]maxSlidingWindow(int[] nums,int k){ArrayDeque<Integer> deque =newArrayDeque<>();int n = nums.length;int[] res =newint[n - k +1];int index =0;for(int i =0; i < n; i++){while(!deque.isEmpty()&& deque.peek()< i - k +1){
deque.poll();}while(!deque.isEmpty()&& nums[deque.peekLast()]< nums[i]){
deque.pollLast();}
deque.offer(i);if(i >= k -1){
res[index++]= nums[deque.peek()];}}return res;}}