数组模拟队列,存放当前窗口最大值

滑动窗口的最大值

http://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788

import java.util.*;
public class Solution {
    public ArrayList<Integer> maxInWindows(int [] num, int size) {
        ArrayList<Integer> res = new ArrayList<>();
        if(size == 0) return res;
        int hh = 0,tt = -1;
        int[] arr = new int[num.length];
        for(int i = 0;i < num.length;i++){
            if(hh <= tt && i - arr[hh] >= size) hh++;
            while(hh <= tt && num[i] > num[arr[tt]]) tt--;
            arr[++tt] = i;
            if(i >= size - 1 && hh <= tt) res.add(num[arr[hh]]);
        }
        return res;
    }
}
全部评论

相关推荐

2025-12-15 14:16
门头沟学院 Java
回家当保安:发offer的时候会背调学信网,最好不要这样。 “27届 ”和“28届以下 ”公司招聘的预期是不一样的。
实习简历求拷打
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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