Skip to content

Commit 34bf540

Browse files
committed
Create 239. Sliding Maximum Window - deque.cpp
1 parent 9bb8123 commit 34bf540

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
4+
deque<int> dq;
5+
vector<int> res;
6+
const size_t n = nums.size();
7+
for(int i = 0; i < n; ++i){
8+
while(!dq.empty() && dq.front() <= i - k) dq.pop_front();
9+
while(!dq.empty() && nums[dq.back()] < nums[i]) dq.pop_back();
10+
dq.push_back(i);
11+
if(i >= k - 1)
12+
res.push_back(nums[dq.front()]);
13+
}
14+
return res;
15+
}
16+
};

0 commit comments

Comments
 (0)