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

滑动窗口的最大值

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

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 17:13
想去,但是听说加班强度实在难崩,所以拒绝了,现在有点心梗对面hr感觉也是实习生,打电话的时候怪紧张的,但是感觉人很好嘞
水中水之下水道的鼠鼠:哥们这不先去体验一下,不行再跑呗,大不了混个实习经历(有更好的转正offer就当我没说)
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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