zoukankan      html  css  js  c++  java
  • 239. Sliding Window Maximum (滑动窗口最大值, 大根堆or 单调队列)

    You are given an array of integers 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 1:

    Input: nums = [1,3,-1,-3,5,3,6,7], 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

     法1:

     1 class Solution {
     2 public:
     3     vector<int> maxSlidingWindow(vector<int>& nums, int k) {
     4         vector<int> res;
     5         priority_queue<pair<int, int>> q;
     6         for(int i = 0; i < k;++i) {
     7             q.push({nums[i],i});
     8         }
     9         res.push_back(q.top().first);
    10 
    11         for(int i = k;i < nums.size();++i) {
    12             q.push({nums[i],i});
    13             while(q.top().second + k <= i) {
    14                 q.pop();
    15             }
    16             res.push_back(q.top().first);
    17         }
    18         return res;
    19     }
    20 };

    法二:

    https://labuladong.gitbook.io/algo/mu-lu-ye-1/mu-lu-ye-2/dan-tiao-dui-lie

    首先,明确一个概念,单调队列是在队列中所有的元素都是单调的,要么单调增,要么单调减,新增元素时到队尾,此时队列不符合单调性就将队尾元素出队,直到队列单调或空。

     1 class MonotonicQueue {
     2 private:
     3     deque<int> data;
     4 public:
     5     void push(int n) {
     6         while (!data.empty() && data.back() < n) 
     7             data.pop_back();
     8         data.push_back(n);
     9     }
    10     
    11     int max() { return data.front(); }
    12     
    13     void pop(int n) {
    14         if (!data.empty() && data.front() == n)
    15             data.pop_front();
    16     }
    17 };
    18 
    19 class Solution {
    20 public:
    21     vector<int> maxSlidingWindow(vector<int>& nums, int k) {
    22     MonotonicQueue window;
    23     vector<int> res;
    24     for (int i = 0; i < nums.size(); i++) {
    25         if (i < k - 1) { //先填满窗口的前 k - 1
    26             window.push(nums[i]);
    27         } else { // 窗口向前滑动
    28             window.push(nums[i]);
    29             res.push_back(window.max());
    30             window.pop(nums[i - k + 1]);
    31         }
    32     }
    33     return res;
    34     }
    35 };
     1 class Solution {
     2 public:
     3     vector<int> maxSlidingWindow(vector<int>& nums, int k) {
     4     deque<int> q;
     5     vector<int> res;
     6     for (int i = 0; i < nums.size(); i++) {
     7         // push
     8         while(!q.empty()&& q.back() < nums[i]) {
     9             q.pop_back();
    10         }
    11         q.push_back(nums[i]);
    12         if (i >= k - 1) { //前k - 1 已经填满
    13             res.push_back(q.front());
    14             // pop
    15             if(!q.empty() && nums[i - k + 1]== q.front()) {
    16                 q.pop_front();
    17             }
    18         }
    19     }
    20     return res;
    21     }
    22 };
  • 相关阅读:
    【转】点集 凸包
    【转】Word中使用Endnote很卡解决方案
    【转】一个程序员的面试经验之谈
    【转】 std list/vector sort 排序
    std::numeric_limits<int>::max() error C2589: '(' : illegal token on right side of '::' 解决办法
    【转】Log4cpp 封装
    【转】 log4cpp 的使用
    遍历目录 删除目录中包含指定字符的文件和文件夹
    mysql主从复制架构
    mysql分区功能详细介绍,以及实例
  • 原文地址:https://www.cnblogs.com/zle1992/p/14801627.html
Copyright © 2011-2022 走看看