<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [LeetCode] 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold 大小為 K 且平均值大于等于閾值的子數組數目


      Given an array of integers arr and two integers k and threshold, return *the number of sub-arrays of size k and average greater than or equal to *threshold.

      Example 1:

      Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
      Output: 3
      Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).

      Example 2:

      Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5
      Output: 6
      Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.

      Constraints:

      • 1 <= arr.length <= 10^5
      • 1 <= arr[i] <= 10^4
      • 1 <= k <= arr.length
      • 0 <= threshold <= 10^4

      這道題給了一個正整數數組 arr,和兩個參數k,threshold。說是讓找出所有長度為k的子數組的個數,使得該子數組的平均值大于等于 threshold。子數組的平均值的計算方法即為子數組之和除以其長度,這里就是除以k。而求子數組之和的問題,博主下意識的就會想到建立累加和數組,這樣可以快速求出任意長度的子數組之和。這里也可以用這種方法,建立累加和數組 sums,然后就是遍歷所有的長度為k的子數組,然后通過 sums[i]-sums[i-k] 快速得到其數組之和。接下來就要進行比較了,這里博主沒有使用除法,因為除法沒有乘法高效,用k乘以 threshold 的結果進行比較也是一樣的,若滿足條件,則結果自增1即可,參見代碼如下:


      解法一:

      class Solution {
      public:
          int numOfSubarrays(vector<int>& arr, int k, int threshold) {
              int res = 0, n = arr.size(), target = k * threshold;
              vector<int> sums(n + 1);
              for (int i = 1; i <= n; ++i) {
                  sums[i] = sums[i - 1] + arr[i - 1];
              }
              for (int i = k; i <= n; ++i) {
                  int sum = sums[i] - sums[i - k];
                  if (sum >= target) ++res;
              }
              return res;
          }
      };
      

      由于這道題只關心長度為k的子數組,那么建立整個累加和數組就略顯多余了,只要維護一個大小為k的窗口就能遍歷所有長度為k的子數組了。這就是滑動窗口 Sliding Window 的思路,遍歷數組,將當前數字加到 sum 中,若i大于等于k,說明此時窗口大小超過了k,需要把左邊界 arr[i-k] 減去,然后把 sum 跟 target 進行比較,注意這里的i必須要大于等于 k-1,因為要保證窗口的大小正好是k,參見代碼如下:


      解法二:

      class Solution {
      public:
          int numOfSubarrays(vector<int>& arr, int k, int threshold) {
              int res = 0, n = arr.size(), sum = 0, target = k * threshold;
              for (int i = 0; i < n; ++i) {
                  sum += arr[i];
                  if (i >= k) sum -= arr[i - k];
                  if (i >= k - 1 && sum >= target) ++res;
              }
              return res;
          }
      };
      

      Github 同步地址:

      https://github.com/grandyang/leetcode/issues/1343


      類似題目:

      K Radius Subarray Averages

      Count Subarrays With Median K


      參考資料:

      https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold

      https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/solutions/503786/c-short-solution/

      https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/solutions/502740/java-python-3-2-codes-prefix-sum-and-sliding-window-w-analysis/


      LeetCode All in One 題目講解匯總(持續更新中...)

      posted @ 2023-05-16 12:36  Grandyang  閱讀(228)  評論(0)    收藏  舉報
      Fork me on GitHub
      主站蜘蛛池模板: CAOPORN免费视频国产| 亚洲欧洲国产综合一区二区| 平邑县| 水蜜桃精品综合视频在线| 中文字幕无码av不卡一区| 亚洲国产精品一二三四区| 产综合无码一区| 国产99在线 | 欧美| av中文字幕在线二区| 精品国产亚洲一区二区三区| 亚洲国模精品一区二区| 蜜桃无码一区二区三区| 综合亚洲网| 午夜福利精品国产二区| 亚洲熟妇少妇任你躁在线观看无码| 亚洲夜色噜噜av在线观看| 色欲av亚洲一区无码少妇| 成人一区二区人妻不卡视频| 国产午夜精品福利91| 国产精品区免费视频| 激情综合网激情五月激情| 国产美熟女乱又伦AV果冻传媒| 聂拉木县| 伦伦影院午夜理论片| 国产精品成人aaaaa网站| 亚洲偷自拍国综合| 男人天堂亚洲天堂女人天堂| 高清自拍亚洲精品二区| 亚洲色拍拍噜噜噜最新网站| 熟女少妇精品一区二区 | 国产乱码日韩精品一区二区| 国产成人精品手机在线观看| 西西444www高清大胆| 国产精品成人一区二区三| 欧美裸体xxxx极品| 亚洲av第一区二区三区| 免费看男女做好爽好硬视频| 老司机精品影院一区二区三区| 无码人妻丰满熟妇啪啪| 一区二区三区四区自拍视频| 国产中文字幕在线一区|