We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5f11145 commit 7a2a549Copy full SHA for 7a2a549
CPP/sliding-window/Sliding Window Maximum.cpp
@@ -0,0 +1,35 @@
1
+class Solution {
2
+public:
3
+ vector<int> maxSlidingWindow(vector<int>& nums, int k) {
4
+
5
+ deque<int> maxi(k);
6
+ vector<int> ans;
7
8
+ for(int i = 0; i < k; i++) {
9
10
+ while(!maxi.empty() && nums[maxi.back()] <= nums[i]) {
11
+ maxi.pop_back();
12
+ }
13
+ maxi.push_back(i);
14
15
+ ans.push_back(nums[maxi.front()]);
16
17
+ for(int i = k; i < nums.size(); i++) {
18
19
+ //removal
20
+ while(!maxi.empty() && i-maxi.front() >= k) {
21
+ maxi.pop_front();
22
23
24
+ //addition
25
26
27
28
29
30
31
32
33
+ return ans;
34
35
+};
0 commit comments