Skip to content

Commit 1567aa4

Browse files
committedSep 11, 2021
Time: 411 ms (26.18%), Space: 131.7 MB (94.77%) - LeetHub
1 parent c98f8e0 commit 1567aa4

File tree

1 file changed

+19
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)
Please sign in to comment.