找每个窗口中的最大值
来源:互联网 发布:淘宝设置客服 编辑:程序博客网 时间:2024/06/07 07:43
A long array A[] is given to you. There is a sliding window of size w
which is moving from the very left of the array to the very right.
You can only see the w numbers in the window. Each time the sliding window
moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and w is 3.
which is moving from the very left of the array to the very right.
You can only see the w numbers in the window. Each time the sliding window
moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and w is 3.
可以用sparse table做,可以用一个长度为w的有序数组,可以用堆
下面的算法比较精巧
考虑可以发现没有必要每次维护w个数,比如当window是1,2,5,4,3,2时, window
右移一位或两位的时候, 答案都是5, 所以,没必要维护全部数据,维护 5432就ok了.考虑
用一个队列来维护窗口,时间复杂度可以做到O(n)
void AddOne(deque<int>& que, int a[], int i){ assert(a && i >= 0); while (!que.empty() && a[que.back()] < a[i]) que.pop_back(); que.push_back(i);}void PrintWindow(int a[], int n, int w){ assert(a && n > 0 && w > 0 && n >= w); deque<int> que; for (int i = 0; i < w; i++) AddOne(que, a, i); cout<<a[que.front()]<<endl; for (int i = 1; i+w-1 < n; i++) { if (i-1 == que.front()) que.pop_front(); AddOne(que, a, i+w-1); cout<<a[que.front()]<<endl; }}
- 找每个窗口中的最大值
- 滑动窗口中的最大值
- 找出滑动窗口数组中的最大值
- 数组滑动窗口中的最大值[单调队列]
- sql查询语句,求每个分组中的最大值实例
- sql查询语句,求每个分组中的最大值实例
- sql查询语句,求每个分组中的最大值实例
- 函数嵌套找最大值
- 胜利树找最大值
- 找最大值与最小值
- 找最大值和最小值
- vector找最大值 最小值
- 找最大值,最小值,累加
- 矩阵找最大值
- 指针+排序找最大值
- 239. Sliding Window Maximum 固定的滑动窗口里找最大值
- FAQ12:每个线程一个消息队列?还是线程中的每个窗口各有一个消息队列?
- 分治法找最大值最小值
- 包含子对象的派生类的构造函数
- HTML -- FrameSet使用
- mysql查询中实现oracle中的rownum函数的效果,返回每行查询结果的行序号
- iOS5的strong,weak,unsafe_unretained ARC自动管理内存
- ios绘图总结
- 找每个窗口中的最大值
- Linux下的压缩解压缩命令详解
- 遥感平台opticks编译总结
- php 数组函数
- Spring 输入国际化
- 关于Handler
- 有的wsdl在eclipse下面生成java代码报错解决办法
- Adapter(适配器)模式
- Jommla2.5如何在组件Component中调用Module