<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)239:滑動窗口最大值

          共 4048字,需瀏覽 9分鐘

           ·

          2021-04-18 18:18

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 滑動窗口最大值,我們先來看題面:
          https://leetcode-cn.com/problems/sliding-window-maximum/

          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.

          給你一個整數(shù)數(shù)組 nums,有一個大小為 k 的滑動窗口從數(shù)組的最左側(cè)移動到數(shù)組的最右側(cè)。你只可以看到在滑動窗口內(nèi)的 k 個數(shù)字?;瑒哟翱诿看沃幌蛴乙苿右晃?。
          返回滑動窗口中的最大值。

          示例


          示例 1:

          輸入: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

          示例 2:

          輸入:nums = [1], k = 1
          輸出:[1]

          示例 3:

          輸入:nums = [1,-1], k = 1
          輸出:[1,-1]

          示例 4:

          輸入:nums = [9,11], k = 2
          輸出:[11]

          示例 5:

          輸入:nums = [4,-2], k = 2
          輸出:[4]


          解題

          https://blog.csdn.net/qq_20880415/article/details/107561091

          思路:采用雙端隊列,保證雙端隊列的第一個值是當(dāng)前窗口的的最大值,代碼主要維護兩個容器,一個vector輸出結(jié)果,一個deque來保存數(shù)組索引值,接下來遍歷數(shù)組,根據(jù)條件把數(shù)組的索引值插到deque當(dāng)中,具體的判斷條件有兩個:

          1、當(dāng)當(dāng)前數(shù)nums[i]大于deque中最后索引對應(yīng)的值,那么刪除這個索引,知道deque中最后索引對應(yīng)的值大于nums[i]為止

          2、當(dāng)當(dāng)前索引 i-que.front()>=k 就表示que.front()不在當(dāng)前i對應(yīng)的滑動窗口中了,于是pop_front,同時在遍歷過程中保存deque front()索引對應(yīng)的最大值

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


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-220題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)221:最大正方形

          LeetCode刷題實戰(zhàn)222:完全二叉樹的節(jié)點個數(shù)

          LeetCode刷題實戰(zhàn)223:矩形面積

          LeetCode刷題實戰(zhàn)224:基本計算器

          LeetCode刷題實戰(zhàn)225:用隊列實現(xiàn)棧

          LeetCode刷題實戰(zhàn)226:翻轉(zhuǎn)二叉樹

          LeetCode刷題實戰(zhàn)227:基本計算器 II

          LeetCode刷題實戰(zhàn)228:匯總區(qū)間

          LeetCode刷題實戰(zhàn)229:求眾數(shù) II

          LeetCode刷題實戰(zhàn)230:二叉搜索樹中第K小的元素

          LeetCode刷題實戰(zhàn)231:2的冪

          LeetCode刷題實戰(zhàn)232:用棧實現(xiàn)隊列

          LeetCode刷題實戰(zhàn)233:數(shù)字 1 的個數(shù)

          LeetCode刷題實戰(zhàn)234:回文鏈表

          LeetCode刷題實戰(zhàn)235:二叉搜索樹的最近公共祖先

          LeetCode刷題實戰(zhàn)236:二叉樹的最近公共祖先

          LeetCode刷題實戰(zhàn)237:刪除鏈表中的節(jié)點

          LeetCode刷題實戰(zhàn)238:除自身以外數(shù)組的乘積


          瀏覽 33
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一级a一级a爰免费免免高潮 | 青青草成人视频在线 | 我操骚逼网 | 美女黄在线看免费日本 | 日韩精品一二区 |