239. Sliding Window Maximum

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. Return the max sliding window.

Example:

Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7] 
Explanation: 

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

Note: 
You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty array.

Follow up:
Could you solve it in linear time?


解法一:

求最大值,可以考虑用heap。可是当sliding window移动时,heap不能找到需要删除的element(only top element is visiable in heap),to solve this issue, we can use a hash table to store the elements of the sliding window: when calculating the max value, check whether the heap's top element is in the hash map or not, if in, it is the max value; otherwise, pop the top and continue until find the max value.

时间复杂度:this is not a linear time algorithm. 

 1 class Solution {
 2 public:
 3     vector<int> maxSlidingWindow(vector<int>& nums, int k) {
 4         priority_queue<int> pq;     //easily to find the max value
 5         unordered_map<int,int> map; //store nums in the sliding window
 6         vector<int> result;
 7         if(nums.size()==0){
 8             return result;
 9         }
10 
11         for(int i=0;i<k;i++){
12             pq.push(nums[i]);
13             map[nums[i]]++;
14         }
15         result.push_back(pq.top());
16         for(int i=k;i<nums.size();i++){
17             pq.push(nums[i]);
18             map[nums[i]]++;
19             map[nums[i-k]]--;
20             if(map[nums[i-k]]==0){
21                 map.erase(nums[i-k]);
22             }
23             
24             //priority queue cannot remove a value easily, we can check whether its top value is in the map
25             while(map.find(pq.top())==map.end()){ 
26                 pq.pop();
27             }
28             result.push_back(pq.top());
29         }
30         return result;
31     }
32 };

 

解法二:linear algorithm

使用双向队列。队列存储数组下标,里面的元素是最大值的下标

 

参考答案:

 1 class Solution {
 2 public:
 3     vector<int> maxSlidingWindow(vector<int>& nums, int k) {
 4         vector<int> result;
 5         deque<int> q; //sliding window's possible max values
 6         for (int i = 0; i < nums.size(); ++i) {
 7             if (!q.empty() && q.front() == i - k) q.pop_front();
 8             
 9             //if current element gets in, elements in dequeue that is smaller need to be removed  
10             while (!q.empty() && nums[q.back()] < nums[i]) q.pop_back(); 
11             q.push_back(i);
12             if (i >= k - 1) result.push_back(nums[q.front()]);
13         }
14         return result;
15     }
16 };

 

posted @ 2018-07-05 09:20  回到明天  阅读(102)  评论(0)    收藏  举报