当前位置:网站首页>Stack and queue - 239. Sliding window maximum
Stack and queue - 239. Sliding window maximum
2022-07-26 00:03:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Give you an array of integers nums, There is a size of k The sliding window of the array moves from the leftmost side to the rightmost side of the array . You can only see... In the sliding window k A digital . The sliding window moves only one bit to the right at a time .
return The maximum value in the sliding window .
2 Title Example

Example 2:
Input :nums = [1], k = 1
Output :[1]
3 Topic tips
1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length
4 Ideas
queue :
about 「 Maximum 」, We can think of — A very suitable data structure , That's the priority queue ( Pile up ), The large root heap can help us maintain in real time — Maximum value in series elements .
For this question , At the beginning , We will array nums Before k Put elements in the priority queue . Whenever we move the window to the right , We can put a new element in the priority queue , At this time, the element at the top of the heap is the maximum value of all elements in the heap . However, this maximum may not be in the sliding window , under these circumstances , This value is in the array nums The position in appears to the left of the left boundary of the sliding window . therefore , When we continue to move the window to the right , This value will never appear in the sliding window , We can permanently remove it from the priority queue . We constantly remove elements from the top of the heap , Until it does appear in the sliding window . here , The top element is the maximum value in the sliding window . In order to judge the position relationship between the heap top element and the sliding window , We can store two tuples in the priority queue (num, indez), Show elements num The subscript in the array is indez.
Complexity analysis
Time complexity :O(n log n), among n It's an array nums The length of . In the worst case , Array nums The elements in are monotonically increasing , Then the final priority queue contains all the elements , No elements have been removed . Because the time complexity of putting an element into the priority queue is O(log n), So the total time complexity is O(n logn).
Spatial complexity :O(n), That is, the space needed by the priority queue . All spatial complexity analyses here do not consider the need for the returned answer O(n) Space , Only calculate the use of additional space .
5 My answer
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
public int compare(int[] pair1, int[] pair2) {
return pair1[0] != pair2[0] ? pair2[0] - pair1[0] : pair2[1] - pair1[1];
}
});
for (int i = 0; i < k; ++i) {
pq.offer(new int[]{
nums[i], i});
}
int[] ans = new int[n - k + 1];
ans[0] = pq.peek()[0];
for (int i = k; i < n; ++i) {
pq.offer(new int[]{
nums[i], i});
while (pq.peek()[1] <= i - k) {
pq.poll();
}
ans[i - k + 1] = pq.peek()[0];
}
return ans;
}
}
边栏推荐
- 开放API生态系统面临的十个威胁
- The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
- 多御安全浏览器手机版将增加新功能,使用户浏览更个性化
- [learning notes] unreal 4 engine introduction (IV)
- 二叉树——257. 二叉树的所有路径
- C语言实战之猜拳游戏
- Imitating the magnifying glass effect of JD products -- JS Foundation
- Sort fake contacts
- Programming password guessing game
- 回溯——17. 电话号码的字母组合
猜你喜欢
随机推荐
统计之歌 歌词
调用钉钉api报错:机器人发送签名过期;solution:签名生成时间和发送时间请保持在 timestampms 以内
模式之固定与交替顺序执行
NVIDIA cuDNN学习
Exercise (3) create a list set (both ArrayList and LinkedList)
Pytorch学习记录(一):Pytorch 简介
Demo of pointer function
二叉树——226. 翻转二叉树
Annotation @autowired source code analysis
二叉树——404. 左叶子之和
Lua脚本编写Wireshark插件解析第三方私有协议
VSCode格式化Json文件
BOM 浏览器对象模型
Weight file and pre training file of yolov3
C# - readonly 和 const 关键字
初阶C语言 - 分支语句(if、switch)
C语言实战之猜拳游戏
浅识 OWASP
[learning notes] solid works operation record
The process of finding free screen recording software - I didn't expect win10 to come with this function






![牛客/洛谷——[NOIP2003 普及组]栈](/img/95/871b1c6f492b467bffd25912304b44.gif)


