Sliding Window Maximum
Screen Shot 2017-09-13 at 16.42.48.png
public class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums == null || nums.length < k || k <= 0) return new int[0];
int n = nums.length;
int[] res = new int[n - k + 1];
int ri = 0;
Deque<Integer> deque = new LinkedList<>();
for(int i = 0; i < n ; i++)
{
while(!deque.isEmpty() && deque.peekFirst() < i - k + 1) deque.pollFirst();
while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) deque.pollLast();
deque.offerLast(i);
if(i >= k - 1) res[ri++] = nums[deque.peekFirst()];
}
return res;
}
}
本文标题: Sliding Window Maximum
本文链接:https://www.haomeiwen.com/subject/mtyrsxtx.html
网友评论