-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path239. Sliding Window Maximum_deque.cc
69 lines (64 loc) · 1.84 KB
/
239. Sliding Window Maximum_deque.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Using deque method
//TC:O(n)
//SC:O(k)
static auto it = [](){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
vector<int> ans;
deque<int> dq;
for (int i = 0; i < n; i++) {
//If i minus k equals dp.front, then the sliding window should move right by one position
if (!dq.empty() && dq.front() == i - k) {
dq.pop_front();
}
//do not add equal sign because the same numbers will be possbile maximum in next round
while (!dq.empty() && nums[dq.back()] < nums[i])
dq.pop_back();
dq.push_back(i);
if (i >= k - 1) {
ans.push_back(nums[dq.front()]);
}
}
return ans;
}
};
//TLE
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
vector<int> ans;
if (n == 1) {
ans.push_back(nums[0]);
return ans;
}
deque<int> dq;
int max_val = INT_MIN;
for (int i = 0; i < n; i++) {
if (i > k - 1) {
if (dq.front() == max_val) {
dq.pop_front();
max_val = INT_MIN;
for (int i = 0; i < dq.size(); i++) {
max_val = max_val > dq[i] ? max_val : dq[i];
}
} else {
dq.pop_front();
}
}
dq.push_back(nums[i]);
max_val = max_val > nums[i] ? max_val : nums[i];
if (i >= k - 1) {
ans.push_back(max_val);
}
}
return ans;
}
};