当前位置:网站首页>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
边栏推荐
- Further study of data visualization
- Webapi + form form upload file
- Detailed explanation of C webpai route
- 线性代数感悟之1
- ASP. Net core configuration options (Part 1)
- 开期货,开户云安全还是相信期货公司的软件?
- Low code development platform sorting
- [batch change MySQL table and corresponding codes of fields in the table]
- 古代埃及希腊,数学用的什么进制
- Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
猜你喜欢
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Halo 开源项目学习(二):实体类与数据表
Using quartz under. Net core - [1] quick start
Future 用法详解
ASP. Net core JWT certification
Why do some people say SCM is simple and I have to learn it so hard?
Net standard
2021长城杯WP
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
Understanding of RPC core concepts
随机推荐
Simulation of infrared wireless communication based on 51 single chip microcomputer
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
[simple understanding of database]
Net standard
Using quartz under. Net core - calendar of [6] jobs and triggers
Model problems of stock in and stock out and inventory system
Baidu Map 3D rotation and tilt angle adjustment
ClickHouse-表引擎
QT modification UI does not take effect
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
The system cannot be started after AHCI is enabled
RPC核心概念理解
Devexpress GridView add select all columns
Matlab / Simulink simulation of double closed loop DC speed regulation system
Understanding and small examples of unity3d object pool
uni-app黑马优购项目学习记录(下)
Shell-awk命令的使用
[related to zhengheyuan cutting tools]
双指针进阶--leetcode题目--盛最多水的容器
Open futures, open an account, cloud security or trust the software of futures companies?