当前位置:网站首页>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;
}
}
边栏推荐
- 二叉树——111. 二叉树的最小深度
- QT smart pointer error prone point
- BOM browser object model
- 如何用yolov5 做个闯红灯监控的智能交通系统(1)
- [learning notes] solid works operation record
- Fixed and alternate sequential execution of modes
- Stm32 systeminit trap during simulation debugging
- 开放API生态系统面临的十个威胁
- Exercise (2) create a set to store the elements "1", "$", "2", "$", "3", "$", "4"“
- Program environment and pretreatment
猜你喜欢

二叉树——112. 路径总和

二叉树——530.二叉搜索树的最小绝对差

什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。

如何用yolov5 做个闯红灯监控的智能交通系统(1)

初阶C语言 - 分支语句(if、switch)

二叉树——101. 对称二叉树

安全文档归档软件
![[learning notes] unreal 4 engine introduction (IV)](/img/30/4defa3cbd785d43adb405c71d16406.png)
[learning notes] unreal 4 engine introduction (IV)

Topsis与熵权法

Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
随机推荐
什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。
通货膨胀之下,后市如何操作?2021-05-14
Scroll case: return to top with animation
LeetCode 刷题系列 -- 931. 下降路径最小和
Lua script Wireshark plug-in to resolve third-party private protocols
Exercise (2) create a set to store the elements "1", "$", "2", "$", "3", "$", "4"“
寻找链表的中间节点
The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
Find the cause of program dead cycle through debugging
C# - readonly 和 const 关键字
Pytoch learning record (I): introduction to pytoch
Leetcode question brushing series -- 931. Minimum sum of descent path
STM32 lighting procedure
【一库】mapbox-gl!一款开箱即用的地图引擎
SIGIR '22 recommendation system paper graph network
开放API生态系统面临的十个威胁
1223. Dice simulation range DP
Topsis与熵权法
C language actual combat guessing game
Nacos offline service times error errcode: 500