{"id":6306,"date":"2020-02-13T22:28:10","date_gmt":"2020-02-14T06:28:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6306"},"modified":"2020-02-13T22:33:02","modified_gmt":"2020-02-14T06:33:02","slug":"leetcode-1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>arr<\/code>&nbsp;and two integers&nbsp;<code>k<\/code>&nbsp;and&nbsp;<code>threshold<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of sub-arrays<\/em>&nbsp;of size&nbsp;<code>k<\/code>&nbsp;and average greater than or equal to&nbsp;<code>threshold<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> 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).\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,1,1,1,1], k = 1, threshold = 0\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [7,7,7,7,7,7,7], k = 7, threshold = 7\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [4,4,4,4], k = 4, threshold = 1\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 10^4<\/code><\/li><li><code>1 &lt;= k &lt;= arr.length<\/code><\/li><li><code>0 &lt;= threshold &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Window size = k<\/li><li>Maintain the sum of the window<\/li><li>Check sum &gt;= threshold * k<\/li><\/ol>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numOfSubarrays(vector<int>& arr, int k, int threshold) {\n    int ans = 0;\n    int sum = 0;\n    for (int i = 0; i < arr.size(); ++i) {\n      sum += arr[i];\n      if (i + 1 >= k) {\n        if (threshold * k <= sum) ++ans;\n        sum -= arr[i + 1 - k];\n      } \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;arr&nbsp;and two integers&nbsp;k&nbsp;and&nbsp;threshold. Return&nbsp;the number of sub-arrays&nbsp;of size&nbsp;k&nbsp;and average greater than or equal to&nbsp;threshold. Example 1: Input: arr = [2,2,2,2,5,5,5,8], k&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[327,177,215,41],"class_list":["post-6306","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-k","tag-medium","tag-sliding-window","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6306"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6306\/revisions"}],"predecessor-version":[{"id":6308,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6306\/revisions\/6308"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}