当前位置:网站首页>Leetcode 239. Sliding window maximum
Leetcode 239. Sliding window maximum
2022-07-19 12:14:00 【LuZhouShiLi】
Leetcode 239. Maximum sliding window
subject
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 .
Ideas
Refer to the explanation of the question :https://leetcode.cn/problems/sliding-window-maximum/solution/tu-jie-suan-fa-dan-diao-dui-lie-zui-jian-h012/
Code
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
deque<int> q;// deque
vector<int>res;
for(int i = 0; i < nums.size(); i++)
{
while(q.size() && i - k + 1 > q.front())
{
q.pop_front();// Judge whether the counterpart is within the sliding window range
}
while(q.size() && nums[i] >= nums[q.back()])
{
// The current element is larger than the tail element
q.pop_back();// Maintain monotonic decreasing queues
}
q.push_back(i);// Insert the current element at the end of the queue
if(i >= k - 1)
{
res.push_back(nums[q.front()]);// The element of the sliding window has reached k individual Insert the header element into the result array
}
}
return res;
}
};
边栏推荐
- 熟悉NestJS (新手篇)
- 【C语言编程8】分支预测器
- Nature子刊 | 地下水固碳速率与寡营养海洋系统固碳速率相近
- LeetCode_216_组合总和Ⅲ
- C语言绘图示例-商标徽标
- psd. JS parsing PSD file
- Nature | the carbon sequestration rate of groundwater is similar to that of oligotrophic marine system
- Tikv memory parameter performance tuning
- Swift 二进制数据与16进制字符串的相互转换
- Example of C language painting - progress bar
猜你喜欢
![[wechat applet] use a thousand hand float - rollback](/img/52/4939ff2a644c46fdb388a64ac111c6.png)
[wechat applet] use a thousand hand float - rollback

任天堂专利显示健身环配件后续产品或已在开发中

Opencv draw a black rectangle and write the serial number

How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example

HCIP(6)
![[original] magisk+shamiko has been tested by app root](/img/85/29367cf544c0f221a5c296cdd8facd.png)
[original] magisk+shamiko has been tested by app root

HCIP(5)

C language drawing example - palette

MAPGIS igserver Kyushu - expand service development under the control of localization environment

Solution: code error: error reported by error could not resolve
随机推荐
Nintendo patent shows that the follow-up products of fitness ring accessories may be under development
任天堂专利显示健身环配件后续产品或已在开发中
[MySQL] add, delete, check and modify MySQL (Advanced)
A simple websocket example
Wi Fi sensing technology and practice based on channel state information
Leetcode 20. 有效的括号
对Rapidly-exploring Random Trees(RRT)路径规划方法的理解
getchar()
Research on Wenhua commodity index
[original] magisk+shamiko has been tested by app root
2022-07-07:Spire.Office 7.7.2 for net 闪亮登场
PPPoE拨号上网
Simple implementation of scrapy keyword crawler (take Xinhuanet and people's network as examples)
Redis Distributed cache - Redis Cluster
Chinese garbled code problem in reading binary files
Gradient button function button drawing C language example
C语言绘图示例-繁花图案
2022年了,跨端技术方案应该怎么选?
Baidu document translation API
Two misunderstandings of digital transformation