{"id":8853,"date":"2021-11-28T08:53:38","date_gmt":"2021-11-28T16:53:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8853"},"modified":"2021-11-28T08:54:03","modified_gmt":"2021-11-28T16:54:03","slug":"leetcode-2090-k-radius-subarray-averages","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-2090-k-radius-subarray-averages\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2090. K Radius Subarray Averages"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;array&nbsp;<code>nums<\/code>&nbsp;of&nbsp;<code>n<\/code>&nbsp;integers, and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>k-radius average<\/strong>&nbsp;for a subarray of&nbsp;<code>nums<\/code>&nbsp;<strong>centered<\/strong>&nbsp;at some index&nbsp;<code>i<\/code>&nbsp;with the&nbsp;<strong>radius<\/strong>&nbsp;<code>k<\/code>&nbsp;is the average of&nbsp;<strong>all<\/strong>&nbsp;elements in&nbsp;<code>nums<\/code>&nbsp;between the indices&nbsp;<code>i - k<\/code>&nbsp;and&nbsp;<code>i + k<\/code>&nbsp;(<strong>inclusive<\/strong>). If there are less than&nbsp;<code>k<\/code>&nbsp;elements before&nbsp;<strong>or<\/strong>&nbsp;after the index&nbsp;<code>i<\/code>, then the&nbsp;<strong>k-radius average<\/strong>&nbsp;is&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p>Build and return&nbsp;<em>an array&nbsp;<\/em><code>avgs<\/code><em>&nbsp;of length&nbsp;<\/em><code>n<\/code><em>&nbsp;where&nbsp;<\/em><code>avgs[i]<\/code><em>&nbsp;is the&nbsp;<strong>k-radius average<\/strong>&nbsp;for the subarray centered at index&nbsp;<\/em><code>i<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>average<\/strong>&nbsp;of&nbsp;<code>x<\/code>&nbsp;elements is the sum of the&nbsp;<code>x<\/code>&nbsp;elements divided by&nbsp;<code>x<\/code>, using&nbsp;<strong>integer division<\/strong>. The integer division truncates toward zero, which means losing its fractional part.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, the average of four elements&nbsp;<code>2<\/code>,&nbsp;<code>3<\/code>,&nbsp;<code>1<\/code>, and&nbsp;<code>5<\/code>&nbsp;is&nbsp;<code>(2 + 3 + 1 + 5) \/ 4 = 11 \/ 4 = 2.75<\/code>, which truncates to&nbsp;<code>2<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/11\/07\/eg1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [7,4,3,9,1,8,5,2,6], k = 3\n<strong>Output:<\/strong> [-1,-1,-1,5,4,4,-1,-1,-1]\n<strong>Explanation:<\/strong>\n- avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before<\/strong> each index.\n- The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37.\n  Using <strong>integer division<\/strong>, avg[3] = 37 \/ 7 = 5.\n- For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) \/ 7 = 4.\n- For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) \/ 7 = 4.\n- avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after<\/strong> each index.\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> nums = [100000], k = 0\n<strong>Output:<\/strong> [100000]\n<strong>Explanation:<\/strong>\n- The sum of the subarray centered at index 0 with radius 0 is: 100000.\n  avg[0] = 100000 \/ 1 = 100000.\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> nums = [8], k = 100000\n<strong>Output:<\/strong> [-1]\n<strong>Explanation:<\/strong> \n- avg[0] is -1 because there are less than k elements before and after index 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= nums[i], k &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<p>We compute i &#8211; k&#8217;s average at position i.<\/p>\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  vector<int> getAverages(vector<int>& nums, int k) {\n    const int n = nums.size();\n    long long sum = 0;\n    vector<int> ans(n, -1);    \n    for (int i = 0; i < n; ++i) {\n      sum += nums[i];\n      if (i >= 2 * k) {\n        ans[i - k] = sum \/ (2 * k + 1);           \n        sum -= nums[i - 2 * k];\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;array&nbsp;nums&nbsp;of&nbsp;n&nbsp;integers, and an integer&nbsp;k. The&nbsp;k-radius average&nbsp;for a subarray of&nbsp;nums&nbsp;centered&nbsp;at some index&nbsp;i&nbsp;with the&nbsp;radius&nbsp;k&nbsp;is the average of&nbsp;all&nbsp;elements in&nbsp;nums&nbsp;between the indices&nbsp;i &#8211; k&nbsp;and&nbsp;i + k&nbsp;(inclusive).&#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":[177,735,215],"class_list":["post-8853","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-medium","tag-moving-average","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8853","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=8853"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8853\/revisions"}],"predecessor-version":[{"id":8855,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8853\/revisions\/8855"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}