LeetCode 239. 滑動視窗最大值

來源:互聯網
上載者:User

標籤:第一個   window   push   code   入隊   span   必須   維護   for   

給定一個數組 nums,有一個大小為 的滑動視窗從數組的最左側移動到數組的最右側。你只可以看到在滑動視窗 k 內的數字。滑動視窗每次只向右移動一位。

返回滑動視窗最大值。

樣本:

輸入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3輸出: [3,3,5,5,6,7] 解釋:   滑動視窗的位置                最大值---------------               -----[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 ≤ k ≤ 輸入數組的大小,且輸入數組不為空白。

一開始使用Brute Force居然也可以過,但是只擊敗了5%的代碼。後來經過思考,可以使用O(1)的空間定義兩個變數curMax和curMaxIndex。對第一個視窗,初始化這兩個變數。然後就是在區間[i, i + k - 1]中尋找最大值。因為之前的最大值是在區間[i - 1, i + k - 2]中找到的,那麼有三種情況:

  1. nums[i + k - 1] >= curMax:新視窗的最右端大於原來的最大值,那麼將最大值替換。
  2. 此時視窗最大值必定小於之前視窗的最大值,如果curMaxIndex >= i,即原來的最大值仍在新視窗內,則也可以。
  3. 此時必須重新在新視窗中找到最大值,以更新curMax和curMaxIndex。
class Solution {public:    vector<int> maxSlidingWindow(vector<int>& nums, int k) {        vector<int> res;        int curMax = INT_MIN, curMaxIndex = -1;        for(int i = 0; i < k; i++) {            if(nums[i] >= curMax) {                curMax = nums[i];                curMaxIndex = i;            }        }        res.push_back(curMax);        for(int i = 1; i <= nums.size() - k; i++) {            //考慮[i, i + k - 1]區間            if(nums[i + k - 1] >= curMax) {                curMax = nums[i + k - 1];                curMaxIndex = i + k - 1;                res.push_back(curMax);            } else if(curMaxIndex >= i) {                res.push_back(curMax);            } else {                curMax = INT_MIN;                for(int j = i; j < i + k; j++) {                    if(nums[j] >= curMax) {                        curMax = nums[j];                        curMaxIndex = j;                    }                }                res.push_back(curMax);            }        }        return nums.size()? res: vector<int>();    }};

這樣子最佳化後可以擊敗68%的代碼,還是可以改進。參考http://www.cnblogs.com/grandyang/p/4656517.html的代碼,維護一個遞減的deque。每當新數要進入隊列時,先看原來的數是不是在新視窗內,不是的話就踢去。然後將deque內比新數小的數都去掉。新數入隊,然後考慮是否將隊頭加入答案中。

注意,deque記憶體的是下標。

class Solution {public:    vector<int> maxSlidingWindow(vector<int>& nums, int k) {        vector<int> res;        deque<int> q;        for (int i = 0; i < nums.size(); ++i) {            if (!q.empty() && q.front() == i - k) q.pop_front();            while (!q.empty() && nums[q.back()] < nums[i]) q.pop_back();            q.push_back(i);            if (i >= k - 1) res.push_back(nums[q.front()]);        }        return res;    }};

 

LeetCode 239. 滑動視窗最大值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.