Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector <int> max_of_subarrays(int *arr, int n, int k)
- {
- // your code here
- for(int i=0;i<k;i++) add(arr[i]);
- vector<int>ans;
- ans.push_back(mx());
- for(int i=k;i<n;i++){
- del(arr[i-k]);
- add(arr[i]);
- ans.push_back(mx());
- }
- return ans;
- }
- private:
- deque<int>dq;
- void add(int ele){
- while(!dq.empty() and dq.back()<ele) dq.pop_back();
- dq.push_back(ele);
- }
- void del(int ele){
- if(!dq.empty() and dq.front()==ele) dq.pop_front();
- }
- int mx(){
- return dq.front();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement