<?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>average Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/average/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/average/</link>
	<description></description>
	<lastBuildDate>Sat, 27 Jun 2020 18:11:20 +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>average Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/average/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1491-average-salary-excluding-the-minimum-and-maximum-salary/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1491-average-salary-excluding-the-minimum-and-maximum-salary/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 27 Jun 2020 18:10:48 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6969</guid>

					<description><![CDATA[<p>Given an array of&#160;unique&#160;integers&#160;salary&#160;where&#160;salary[i]&#160;is the salary of the employee&#160;i. Return the average salary of employees excluding the minimum and maximum salary. Example 1: Input: salary&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1491-average-salary-excluding-the-minimum-and-maximum-salary/">花花酱 LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary</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>Given an array of&nbsp;<strong>unique</strong>&nbsp;integers&nbsp;<code>salary</code>&nbsp;where&nbsp;<code>salary[i]</code>&nbsp;is the salary of the employee&nbsp;<code>i</code>.</p>



<p>Return the average salary of employees excluding the minimum and maximum salary.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> salary = [4000,3000,1000,2000]
<strong>Output:</strong> 2500.00000
<strong>Explanation: </strong>Minimum salary and maximum salary are 1000 and 4000 respectively.
Average salary excluding minimum and maximum salary is (2000+3000)/2= 2500
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> salary = [1000,2000,3000]
<strong>Output:</strong> 2000.00000
<strong>Explanation: </strong>Minimum salary and maximum salary are 1000 and 3000 respectively.
Average salary excluding minimum and maximum salary is (2000)/1= 2000
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> salary = [6000,5000,4000,3000,2000,1000]
<strong>Output:</strong> 3500.00000
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> salary = [8000,9000,2000,3000,6000,1000]
<strong>Output:</strong> 4750.00000
</pre>



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



<ul><li><code>3 &lt;= salary.length &lt;= 100</code></li><li><code>10^3&nbsp;&lt;= salary[i] &lt;= 10^6</code></li><li><code>salary[i]</code>&nbsp;is unique.</li><li>Answers within&nbsp;<code>10^-5</code>&nbsp;of the actual value will be accepted as correct.</li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



<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:
  double average(vector&lt;int&gt;&amp; salary) {
    auto [lit, hit] = minmax_element(begin(salary), end(salary));
    int sum = accumulate(begin(salary), end(salary), 0);
    return (sum - *lit - *hit) * 1.0 / (salary.size() - 2);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1491-average-salary-excluding-the-minimum-and-maximum-salary/">花花酱 LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary</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/algorithms/array/leetcode-1491-average-salary-excluding-the-minimum-and-maximum-salary/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 643. Maximum Average Subarray I</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-643-maximum-average-subarray-i/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-643-maximum-average-subarray-i/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 16 Jul 2018 14:25:48 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[sliding window]]></category>
		<category><![CDATA[subarray]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3183</guid>

					<description><![CDATA[<p>Problem 题目大意：找出k长度的子数组的平均值的最大值。 https://leetcode.com/problems/maximum-average-subarray-i/description/ Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-643-maximum-average-subarray-i/">花花酱 LeetCode 643. Maximum Average Subarray I</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>题目大意：找出k长度的子数组的平均值的最大值。</p>
<p><a href="https://leetcode.com/problems/maximum-average-subarray-i/description/">https://leetcode.com/problems/maximum-average-subarray-i/description/</a></p>
<p>Given an array consisting of <code>n</code> integers, find the contiguous subarray of given length <code>k</code> that has the maximum average value. And you need to output the maximum average value.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> [1,12,-5,-6,50,3], k = 4
<b>Output:</b> 12.75
<b>Explanation:</b> Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
</pre>
<p><b>Note:</b></p>
<ol>
<li>1 &lt;= <code>k</code> &lt;= <code>n</code> &lt;= 30,000.</li>
<li>Elements of the given array will be in the range [-10,000, 10,000].</li>
</ol>
<h1><strong>Solution: Sliding Window</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 124 ms
class Solution {
public:
  double findMaxAverage(vector&lt;int&gt;&amp; nums, int k) {
    const int n = nums.size();
    int sum = 0;
    int ans = INT_MIN;
    for (int i = 0; i &lt; n; ++i) {
      if (i &gt;= k) sum -= nums[i - k];
      sum += nums[i];      
      if (i + 1 &gt;= k) ans = max(ans, sum);      
    }
    return static_cast&lt;double&gt;(ans) / k;
  }
};</pre><p></p>
<h1><strong>Related </strong><b>Problems</b></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/heap/leetcode-239-sliding-window-maximum/">花花酱 LeetCode 239. Sliding Window Maximum</a></li>
<li><a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-813-largest-sum-of-averages/">花花酱 LeetCode 813. Largest Sum of Averages</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-643-maximum-average-subarray-i/">花花酱 LeetCode 643. Maximum Average Subarray I</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/algorithms/array/leetcode-643-maximum-average-subarray-i/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 813. Largest Sum of Averages</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-813-largest-sum-of-averages/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-813-largest-sum-of-averages/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 09 Apr 2018 16:13:04 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[partial sum]]></category>
		<category><![CDATA[partition]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2449</guid>

					<description><![CDATA[<p>Problem 题目大意：把一个数组分成k个段，求每段平均值和的最大值。 https://leetcode.com/problems/largest-sum-of-averages/description/ We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-813-largest-sum-of-averages/">花花酱 LeetCode 813. Largest Sum of 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><iframe width="500" height="375" src="https://www.youtube.com/embed/IPdShoUE9z8?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>题目大意：把一个数组分成k个段，求每段平均值和的最大值。</p>
<p><a href="https://leetcode.com/problems/largest-sum-of-averages/description/">https://leetcode.com/problems/largest-sum-of-averages/description/</a></p>
<p>We partition a row of numbers <code>A</code> into at most <code>K</code> adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?</p>
<p>Note that our partition must use every number in A, and that scores are not necessarily integers.</p>
<pre class="crayon:false"><strong>Example:</strong>
<strong>Input:</strong> 
A = [9,1,2,3,9]
K = 3
<strong>Output:</strong> 20
<strong>Explanation:</strong> 
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>1 &lt;= A.length &lt;= 100</code>.</li>
<li><code>1 &lt;= A[i] &lt;= 10000</code>.</li>
<li><code>1 &lt;= K &lt;= A.length</code>.</li>
<li>Answers within <code>10^-6</code> of the correct answer will be accepted as correct.</li>
</ul>
<h1><strong>Idea</strong></h1>
<p><img class="alignnone size-full wp-image-2459" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/04/813-ep179.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/04/813-ep179.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/04/813-ep179-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/04/813-ep179-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p>DP, use dp[k][i] to denote the largest average sum of partitioning first i elements into k groups.</p>
<h3><strong>Init</strong></h3>
<p>dp[1][i] = sum(a[0] ~ a[i &#8211; 1]) / i, for i in 1, 2, &#8230; , n.</p>
<h3><strong>Transition</strong></h3>
<p>dp[k][i] = max(dp[k &#8211; 1][j] + sum(a[j] ~ a[i &#8211; 1]) / (i &#8211; j)) for j in k &#8211; 1,&#8230;,i-1.</p>
<p>that is find the best j such that maximize dp[k][i]</p>
<p>largest sum of partitioning first j elements (a[0] ~ a[j &#8211; 1]) into k &#8211; 1 groups (already computed)</p>
<p>+ average of a[j] ~ a[i &#8211; 1] (partition a[j] ~ a[i &#8211; 1] into 1 group).</p>
<h3><strong>Answer</strong></h3>
<p>dp[K][n]</p>
<p><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"> </ins></p>
<h1><strong>Solution 1: DP</strong></h1>
<p>Time complexity: O(kn^2)</p>
<p>Space complexity: O(kn)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 9 ms
class Solution {
public:
  double largestSumOfAverages(vector&lt;int&gt;&amp; A, int K) {
    const int n = A.size();
    vector&lt;vector&lt;double&gt;&gt; dp(K + 1, vector&lt;double&gt;(n + 1, 0.0));
    vector&lt;double&gt; sums(n + 1, 0.0);
    for (int i = 1; i &lt;= n; ++i) {
      sums[i] = sums[i - 1] + A[i - 1];
      dp[1][i] = static_cast&lt;double&gt;(sums[i]) / i;
    }
    for (int k = 2; k &lt;= K; ++k)
      for (int i = k; i &lt;= n; ++i)
        for (int j = k - 1; j &lt; i; ++j)
          dp[k][i] = max(dp[k][i], dp[k - 1][j] + (sums[i] - sums[j]) / (i - j));
    return dp[K][n];
  }
};</pre><p></div><h2 class="tabtitle">C++ O(n) space</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  double largestSumOfAverages(vector&lt;int&gt;&amp; A, int K) {
    const int n = A.size();
    vector&lt;double&gt; dp(n + 1, 0.0);
    vector&lt;double&gt; sums(n + 1, 0.0);
    for (int i = 1; i &lt;= n; ++i) {
      sums[i] = sums[i - 1] + A[i - 1];
      dp[i] = static_cast&lt;double&gt;(sums[i]) / i;
    }
    for (int k = 2; k &lt;= K; ++k) {
      vector&lt;double&gt; tmp(n + 1, 0.0);
      for (int i = k; i &lt;= n; ++i)
        for (int j = k - 1; j &lt; i; ++j)
          tmp[i] = max(tmp[i], dp[j] + (sums[i] - sums[j]) / (i - j));
      dp.swap(tmp);
    }
    return dp[n];
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: DFS + memoriation </strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 10 ms
class Solution {
public:
  double largestSumOfAverages(vector&lt;int&gt;&amp; A, int K) {
    const int n = A.size();
    m_ = vector&lt;vector&lt;double&gt;&gt;(K + 1, vector&lt;double&gt;(n + 1, 0.0));
    sums_ = vector&lt;double&gt;(n + 1, 0.0);
    for (int i = 1; i &lt;= n; ++i)
      sums_[i] = sums_[i - 1] + A[i - 1];
    return LSA(A, n, K);
  }
private:
  vector&lt;vector&lt;double&gt;&gt; m_;
  vector&lt;double&gt; sums_;
  
  // Largest sum of averages for first n elements in A paritioned into k groups
  double LSA(const vector&lt;int&gt;&amp; A, int n, int k) {
    if (m_[k][n] &gt; 0) return m_[k][n];
    if (k == 1) return sums_[n] / n;
    for (int i = k - 1; i &lt; n; ++i)
      m_[k][n] = max(m_[k][n], LSA(A, i, k - 1) + (sums_[n] - sums_[i]) / (n - i));
    return m_[k][n];
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-813-largest-sum-of-averages/">花花酱 LeetCode 813. Largest Sum of 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/dynamic-programming/leetcode-813-largest-sum-of-averages/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 805. Split Array With Same Average</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-805-split-array-with-same-average/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-805-split-array-with-same-average/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 25 Mar 2018 02:46:20 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2365</guid>

					<description><![CDATA[<p>Problem 题目大意：问能否将一个数组分成两部分，每部分的平均值相同。 In a given integer array A, we must move every element of A to either list B or list C. (B and C&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-805-split-array-with-same-average/">花花酱 LeetCode 805. Split Array With Same Average</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>题目大意：问能否将一个数组分成两部分，每部分的平均值相同。</p>
<p>In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.)</p>
<p>Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty.</p>
<pre class="crayon:false "><strong>Example :</strong>
<strong>Input:</strong> 
[1,2,3,4,5,6,7,8]
<strong>Output:</strong> true
<strong>Explanation: </strong>We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have the average of 4.5.
</pre>
<p><strong>Note:</strong></p>
<ul>
<li>The length of <code>A</code> will be in the range [1, 30].</li>
<li><code>A[i]</code> will be in the range of <code>[0, 10000]</code>.</li>
</ul>
<h1><strong>Solution: Search</strong></h1>
<p>Time complexity: O(2^n)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 771 ms
class Solution {
public:
  bool splitArraySameAverage(vector&lt;int&gt;&amp; A) {
    std::sort(A.begin(), A.end());
    sum = std::accumulate(A.begin(), A.end(), 0);
    n = A.size();
    return dfs(A, 1, 0, 0);
  }
private:
  int sum;
  int n;
  bool dfs(const vector&lt;int&gt;&amp; A, int c, int s, int cur) {
    if (c &gt; A.size() / 2) return false;
    for (int i = s; i &lt; A.size(); ++i) {
      cur += A[i];      
      if (cur * (n - c)  == (sum - cur) * c) return true;
      if (cur * (n - c)  &gt; (sum - cur) * c) break;
      if (dfs(A, c + 1, i + 1, cur)) return true;
      cur -= A[i];
    }
    return false;
  } 
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-805-split-array-with-same-average/">花花酱 LeetCode 805. Split Array With Same Average</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/searching/leetcode-805-split-array-with-same-average/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
