当前位置:网站首页>239. Maximum value of sliding window (difficult) - one-way queue, large top heap - byte skipping high frequency problem
239. Maximum value of sliding window (difficult) - one-way queue, large top heap - byte skipping high frequency problem
2022-04-23 17:33:00 【hequnwang10】
One 、 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 .
Example 1:
Input :nums = [1,3,-1,-3,5,3,6,7], k = 3
Output :[3,3,5,5,6,7]
explain :
Position of sliding window Maximum
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Example 2:
Input :nums = [1], k = 1
Output :[1]
Two 、 Problem solving
One way queue
First of all, this question needs to pay attention to two points
- Maintain the sliding window length value , The sliding window value is k;
- Maintain one-way queue and save subscript value , Left and right Subscripts ,[ Zuo Da , The lower right ], The subscript value on the left represents the largest value , The smallest on the right , Make the elements in the queue from large to small .
When the current value is greater than the end value in the one-way queue , Delete tail value , Save the element values of the one-way queue in descending order , If the interval value of the left and right subscripts exceeds the sliding window value , The left boundary is updated .
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int length = nums.length;
// The queue holds subscript values , The interval of subscript value to maintain the size and length of sliding window
Deque<Integer> queue = new LinkedList<>();
// Initialize sliding window
for(int i = 0;i<k;i++){
// Ensure that the subscript of the sliding window [ Zuo Da , Right small ]. The current value is greater than the end of line element Then update the maximum value
while(!queue.isEmpty() && nums[i] >= nums[queue.peekLast()]){
queue.pollLast();
}
// Add elements to the end of the team
queue.addLast(i);
}
int[] res = new int[length - k + 1];
res[0] = nums[queue.peekFirst()];
for(int i = k;i<length;i++){
// If the current value is greater than the end of line element , Then go straight out of the team ,
while(!queue.isEmpty() && nums[i] >= nums[queue.peekLast()]){
queue.pollLast();
}
// Save the current subscript at the end of the queue
queue.addLast(i);
// Maintain the size of the sliding window
while(queue.peekFirst() <= i - k){
queue.pollFirst();
}
res[i-k+1] = nums[queue.peekFirst()];
}
return res;
}
}
Big pile top
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
// Big pile top
int n = nums.length;
PriorityQueue<int[]> pq = new PriorityQueue<>(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;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009181.html
边栏推荐
- C# Task. Delay and thread The difference between sleep
- Deep understanding of control inversion and dependency injection
- Websocket (basic)
- How to manually implement the mechanism of triggering garbage collection in node
- Node template engine (EJS, art template)
- 122. 买卖股票的最佳时机 II-一次遍历
- 402. 移掉 K 位数字-贪心
- [logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
- Promise (III)
- ClickHouse-数据类型
猜你喜欢

EF core in ASP Generate core priority database based on net entity model

索引:手把手教你索引从零基础到精通使用

ASP. Net core JWT certification

470. 用 Rand7() 实现 Rand10()

Learning record of uni app dark horse yougou project (Part 2)
![Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers](/img/65/89473397da4217201eeee85aef3c10.png)
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers

Double pointer advanced -- leetcode title -- container with the most water

ASP. NET CORE3. 1. Solution to login failure after identity registers users

2021长城杯WP

Net standard
随机推荐
92. 反转链表 II-字节跳动高频题
QT modification UI does not take effect
198. 打家劫舍-动态规划
Header built-in object
索引:手把手教你索引从零基础到精通使用
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
读《Software Engineering at Google》(15)
ECMAScript history
Input file upload
394. 字符串解码-辅助栈
Router object, route object, declarative navigation, programmed navigation
Halo 开源项目学习(二):实体类与数据表
For the space occupation of the software, please refer to the installation directory
41. The first missing positive number
Solution of Navicat connecting Oracle library is not loaded
[C] thoroughly understand the deep copy
剑指 Offer 03. 数组中重复的数字
402. 移掉 K 位数字-贪心
C listens for WMI events
Net standard