Created
March 3, 2015 05:40
-
-
Save rohith2506/683534e0c568d101389e to your computer and use it in GitHub Desktop.
Maximum sliding window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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