题解 | #滑动窗口的最大值#

滑动窗口的最大值

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;
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
码农索隆:卡学历都不行了,开始卡颜值了
点赞 评论 收藏
分享
嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 11:29
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务