当前位置:网站首页>59 - I. maximum value of sliding window
59 - I. maximum value of sliding window
2022-06-12 05:19:00 【Be your goat】
The finger of the sword Offer 59 - I. Maximum sliding window
Ideas : A monotonous queue
The data structure corresponding to the window is a double ended queue , When traversing an array , Each round is guaranteed to be monotonous deque:
deque Contains only the elements in the window ⇒ \Rightarrow ⇒ Every window slide removes elements nums[i-k]
deque Inner non strict decreasing ⇒ \Rightarrow ⇒ Each round of window sliding requires that the number of windows in the queue is less than nums[i] Element pop-up
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ans;
deque<int> q;
for(int i=0;i<nums.size();++i){
if(q.size()&&q.front()<=i-k) q.pop_front();
while(q.size()&&nums[q.back()]<nums[i]) q.pop_back();
q.push_back(i);
if(i>=k-1){
ans.push_back(nums[q.front()]);
}
}
return ans;
}
};
Time complexity O(n)
Spatial complexity O(k)
边栏推荐
- Pytorch was reported by a large number of netizens that torchrec, a new library, was "born" and has a large scale
- Abstract methods and interfaces
- Data processing and data set preparation
- 12.24 day exercise -- Programming summation, 99 multiplication table, while loop and for loop exercises
- The master programmer "plays" a C program that is not like C
- Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
- [getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e
- 加速訓練之並行化 tf.data.Dataset 生成器
- The most commonly used objective weighting method -- entropy weight method
- Can‘t find a suitable configuration file in this directory or any parent. Error reporting and resolution
猜你喜欢

4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)

Map coordinate conversion of Baidu map API

2022-02-28 WPF upper computer 126 understand mqtt

Image processing 13- calculation of integral diagram

Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021

Ubunt 20.04 uses CDROM or ISO as the installation source

The most commonly used objective weighting method -- entropy weight method
![February 19, 2022 [Nolan] Nolan resurrected? Change · Nolan [soul orchid] can be connected to XDD / silly girl](/img/1a/ab2158a532683632f3a12fe41812f5.jpg)
February 19, 2022 [Nolan] Nolan resurrected? Change · Nolan [soul orchid] can be connected to XDD / silly girl
![[cjson] precautions for root node](/img/6c/4462bc8126935f00a5c64f18de13ea.png)
[cjson] precautions for root node

Multi thread learning 4. Sleep, wait, yield, join (), ThreadGroup control the running of threads
随机推荐
【cjson】根节点注意事项
Day18 creation and restoration of sparse array
Static keyword and inheritance, polymorphic and special classes
Design of a simple embedded web service application
JS controls the display and hiding of tags through class
Some optimization methods for UI Application of Qt5 on Hisilicon security platform
asp. Net core theme Middleware
Applet pull-down load refresh onreachbottom
Shandong University network security range experimental platform -- team and project introduction
Fundamentals of intensive learning openai gym environment construction demo
Servlet core
WiFi smartconfig implementation
Summary of common interview questions in redis
Force/release learning arrangement in IC Verification (5) research on the influence of reg type signals
Overview of common classes
Walking "daily question" and "DP"
What is reverse repurchase of treasury bonds? Is the reverse repurchase of treasury bonds safe?
Acquisition of Lai data, NPP data, GPP data and vegetation coverage data
Big manufacturers compete to join rust, performance and safety are the key, and the 2021 rust developer survey report is announced
Nbiot module me3616 at command mqtt connecting thingsboard