-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_239_GFG_SlidingWinMaximum.cpp.cpp
174 lines (135 loc) · 3.7 KB
/
LC_239_GFG_SlidingWinMaximum.cpp.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
https://leetcode.com/problems/sliding-window-maximum/
239. Sliding Window Maximum
https://practice.geeksforgeeks.org/problems/maximum-of-all-subarrays-of-size-k3101/1#
Maximum of all subarrays of size k
https://practice.geeksforgeeks.org/problems/deee0e8cf9910e7219f663c18d6d640ea0b87f87/1/#
*/
// LC ------------------------------------------------------------------
class Solution {
public:
vector<int> maxSlidingWindow_(vector<int>& nums, int k) {
vector<int> ans;
multiset<int> ms;
int i=0, j=0;
while(j<k)
ms.insert(nums[j++]);
ans.push_back(*ms.rbegin());
while(j<nums.size())
{
ms.erase(ms.find(nums[i]));
ms.insert(nums[j]);
ans.push_back(*ms.rbegin());
i++; j++;
}
return ans;
}//end
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ans;
list<int> dq;
int i=0, j=0;
while(j<nums.size())
{
while(!dq.empty() && nums[j] > nums[dq.back()])
dq.pop_back();
dq.push_back(j);
if(j-i+1 < k){
j++;
continue;
}
ans.push_back(nums[dq.front()]);
if(i == dq.front())
dq.pop_front();
i++;
j++;
}
return ans;
}//end
};
//GFG -----------------------------------------------------------
// { Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution
{
public:
//Function to find maximum of each subarray of size k.
//TLE
vector <int> max_of_subarrays_TLE(int *arr, int n, int k)
{
vector<int> ans;
int mx = 0;
for(int i=0; i<n-k+1; i++)
{
mx = arr[i];
for(int j=i; j<i+k; j++)
mx = max(mx, arr[j]);
ans.push_back(mx);
}
return ans;
}
//using multiset
vector <int> max_of_subarrays_(int *arr, int n, int k)
{
vector<int> ans;
multiset<int> ms;
int i=0, j=0;
while(j<k)
ms.insert(arr[j++]);
ans.push_back(*ms.rbegin());
while(j<n)
{
ms.erase(ms.find(arr[i])) ;//a.erase(a.find()) – Remove only one instance of element from multiset having same value
ms.insert(arr[j]);
ans.push_back(*ms.rbegin());
j++;
i++;
}
return ans;
}
vector <int> max_of_subarrays(int *arr, int n, int k)
{
vector<int> ans;
list<int> dq;
int i=0, j=0;
while(j<n)
{
while(!dq.empty() && arr[j] > dq.back())
dq.pop_back();
dq.push_back(arr[j]);
if(j-i+1 < k)
{
j++;
continue;
}
//j-i+1 == k
ans.push_back(dq.front());
if(arr[i] == dq.front())
dq.pop_front();
j++;
i++;
}
return ans;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--){
int n, k;
cin >> n >> k;
int arr[n];
for(int i = 0;i<n;i++)
cin >> arr[i];
Solution ob;
vector <int> res = ob.max_of_subarrays(arr, n, k);
for (int i = 0; i < res.size (); i++)
cout << res[i] << " ";
cout << endl;
}
return 0;
} // } Driver Code Ends