Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
For example,
Given nums = [1,3,-1,-3,5,3,6,7]
, and k = 3.
Window position Max --------------- ----- [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7
Therefore, return the max sliding window as [3,3,5,5,6,7]
.
Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array‘s size for non-empty array.
Follow up:
Could you solve it in linear time?
1 class Solution { 2 public: 3 vector<int> maxSlidingWindow(vector<int>& nums, int k) { 4 vector<int> res; 5 deque<int> q; 6 for(int i = 0; i < nums.size(); i++){ 7 if(!q.empty() && q.front() == i - k) q.pop_front(); 8 while(!q.empty() && nums[q.back()] < nums[i]) q.pop_back();//while!!!! 只保留windows里面最大的在队首 9 q.push_back(i);//deque save the index 10 if(i >= k - 1) res.push_back(nums[q.front()]); 11 } 12 return res; 13 } 14 };
原文:http://www.cnblogs.com/93scarlett/p/6362423.html