LeetCode 239. Sliding Window Maximum

来源:互联网 发布:超级基因优化液女主角 编辑:程序博客网 时间:2024/06/08 08:37

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 thek 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].



#include <iostream>#include <vector>#include <queue>using namespace std;CORRECT// first, we can slove it using priority_queue.<strong>// time complexity: O(nklogk)</strong>vector<int> maxSlidingWindow(vector<int>& nums, int k) {    int n = nums.size();    vector<int> res;    k = k % n;    priority_queue< int, vector<int>, less<int> > maxHeap;    for(int i = 0; i <= n - k; ++i) {        for(int j = i; j < i + k; ++j) {            maxHeap.push(nums[j]);        }        res.push_back(maxHeap.top());        maxHeap = priority_queue <int>();    }    return res;}

WRONG!! I was thinking to use a queue. but it failed on this test case :[1,3,1,2,0,5]  ---> it should output:[3,3,2,5]. but the following code output: [3,3,1,5]

No Idea till now how to make it work.

// we can use a queue to always keep the current max number at the front.vector<int> maxSlidingWindowII(vector<int>& nums, int k) {    queue<int> maxValues;    vector<int> res;    for(int i = 0; i < k; ++i) {        if(maxValues.empty()) {            maxValues.push(i);        } else {            while(!maxValues.empty()) {                if(nums[i] > nums[maxValues.front()]) {                    maxValues.pop();                    continue;                } else break;            }            maxValues.push(i);        }    }    res.push_back(nums[maxValues.front()]);    for(int i = k; i < nums.size(); ++i) {        if(maxValues.empty()) {            maxValues.push(i);        } else {            while(!maxValues.empty() && ((i - maxValues.front()) >= k)) maxValues.pop();             while(!maxValues.empty()) {                if(nums[i] > nums[maxValues.front()]) {                    maxValues.pop();                } else break;            }            maxValues.push(i);        }        res.push_back(nums[maxValues.front()]);    }    return res;}


CORRECT!  using a fixed size queue and a deque.....

vector<int> maxSlidingWindowIII(vector<int>& nums, int k) {    if(nums.size() == 0) return {};    queue<int> window;    deque<int> maxes;    vector<int> maxValue;    for(int i = 0; i < nums.size(); ++i) {        window.push(nums[i]);        while(!maxes.empty() && (nums[i] > maxes.back())) {            maxes.pop_back();        }        maxes.push_back(nums[i]);        if(window.size() == k) {            maxValue.push_back(maxes.front());            int tmp = window.front();            window.pop();            if(tmp == maxes.front()) {                maxes.pop_front();            }        }    }    return maxValue;}int main(void) {    vector<int> nums{1, 3, 1, 2, 0, 5};    vector<int> res = maxSlidingWindowIII(nums, 3);    for(int i = 0; i  < res.size(); ++i) {        cout << res[i] << endl;    }}

A fantastic way!!!!

// this is a very fantastic way to solve it!~/*    given [1, 2, -1, -3, 4, 2, 5, 3], window = 4   minValue [-1, -3, -3, -3, 2, 2]   maxValue [ 2,  2,  4,  4, 5, 5]*//*  cut the array in nums.size() / M segment. For each segment, we traverse from the left, right to get the minValue.  [1, 2, -1,  -3, 4, 2,    5, 3]  leftMin:  [1,  1, -1,   -3, -3, -3,  5, 3]  rightMin:  [-1, -1, -1,  -3,  2,  2,  3, 3]    The final min:  [-1, -3, -3, -3, 2, 2]  leftMax:  [1, 2, 2,  -3, 4, 4,   5, 5]  rightMax:  [2, 2, -1,  4, 4, 2,   5, 3]  The final max:  [2, 2, 4, 4, 5, 5]*/vector<int> getMinWindow(vector<int>& nums, int window) {  int n = nums.size();  vector<int> leftMin(nums.size(), 0);  vector<int> rightMin(nums.size(), 0);  leftMin[0] = nums[0];  rightMin[n - 1] = nums[n - 1];  for(int i = 0; i < n; ++i) {    leftMin[i] = (i % window == 0) : nums[i] : min(leftMin[i-1], nums[i]);    int j = n - i - 1;    rightMin[j] = (j % window == 0) : nums[j] : min(rightMin[j + 1], nums[j]);  }  vector<int> sliding_window(n - window + 1, 0);  for(int i = 0, j = 0; i + window < n; ++i) {    sliding_window[j++] = min(rightMin[i], leftMin[i + w - 1]);  }  return sliding_window;}

0 0
原创粉丝点击