Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 3, 2015 05:40
Show Gist options
  • Save rohith2506/683534e0c568d101389e to your computer and use it in GitHub Desktop.
Save rohith2506/683534e0c568d101389e to your computer and use it in GitHub Desktop.
Maximum sliding window
// Using deque
void max_slid_window(vector<int> v){
deque<int> q;
for(int i=0; i<k; i++){
while(!q.empty() && v[i] >= v[q.back()])
q.pop_back();
q.push_back(i);
}
for(int i=k+1; i<n; i++){
cout << v[q.front()] << " ";
while(!q.empty() && q.front() >= (i-k))
q.pop_front();
while(!q.empty() && v[i] >= v[q.back()])
q.pop_back();
q.push_back(i);
}
cout << v[q.front()] << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment