题解 | #滑动窗口的最大值#
滑动窗口的最大值
http://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
大顶堆
import java.util.*;
public class Solution {
public ArrayList<Integer> maxInWindows(int [] num, int size) {
ArrayList<Integer> list = new ArrayList();
if(size > num.length || size == 0)
return list;
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>((o1,o2)->o2-o1);
for(int i=0; i<num.length; i++){
maxHeap.add(num[i]);
if(maxHeap.size() == size){
list.add(maxHeap.peek());
maxHeap.remove(num[i+1-size]);
}
}
return list;
}
}