<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>moving average Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/moving-average/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/moving-average/</link>
	<description></description>
	<lastBuildDate>Sun, 28 Nov 2021 16:54:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>moving average Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/moving-average/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2090. K Radius Subarray Averages</title>
		<link>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2090-k-radius-subarray-averages/</link>
					<comments>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2090-k-radius-subarray-averages/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Nov 2021 16:53:38 +0000</pubDate>
				<category><![CDATA[Sliding Window]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[moving average]]></category>
		<category><![CDATA[sliding window]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8853</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;array&#160;nums&#160;of&#160;n&#160;integers, and an integer&#160;k. The&#160;k-radius average&#160;for a subarray of&#160;nums&#160;centered&#160;at some index&#160;i&#160;with the&#160;radius&#160;k&#160;is the average of&#160;all&#160;elements in&#160;nums&#160;between the indices&#160;i - k&#160;and&#160;i + k&#160;(inclusive).&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-2090-k-radius-subarray-averages/">花花酱 LeetCode 2090. K Radius Subarray Averages</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<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>



<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>



<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>



<ul><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>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3
<strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1]
<strong>Explanation:</strong>
- avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index.
- The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37.
  Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5.
- For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4.
- For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4.
- avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [100000], k = 0
<strong>Output:</strong> [100000]
<strong>Explanation:</strong>
- The sum of the subarray centered at index 0 with radius 0 is: 100000.
  avg[0] = 100000 / 1 = 100000.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [8], k = 100000
<strong>Output:</strong> [-1]
<strong>Explanation:</strong> 
- avg[0] is -1 because there are less than k elements before and after index 0.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Sliding Window</strong></h2>



<p>We compute i &#8211; k&#8217;s average at position i.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; getAverages(vector&lt;int&gt;&amp; nums, int k) {
    const int n = nums.size();
    long long sum = 0;
    vector&lt;int&gt; ans(n, -1);    
    for (int i = 0; i &lt; n; ++i) {
      sum += nums[i];
      if (i &gt;= 2 * k) {
        ans[i - k] = sum / (2 * k + 1);           
        sum -= nums[i - 2 * k];
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-2090-k-radius-subarray-averages/">花花酱 LeetCode 2090. K Radius Subarray Averages</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2090-k-radius-subarray-averages/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
