We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
void printMaxInSubarrays(const deque& dq, int k) {
deque<int> indices; // To store indices of potential maximums
for (int i = 0; i < dq.size(); ++i) {
// Remove indices that are out of the current window
while (!indices.empty() && indices.front() <= i - k) {
indices.pop_front();
}
// Remove elements that are less than the current element
while (!indices.empty() && dq[indices.back()] <= dq[i]) {
indices.pop_back();
}
// Add current element's index at the end of the deque
indices.push_back(i);
// The first k-1 windows will not be complete, so skip printing max
if (i >= k - 1) {
cout << dq[indices.front()] << " ";
}
}
cout << endl;
Deque-STL
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
using namespace std;
void printMaxInSubarrays(const deque& dq, int k) {
}
int main(){
}