<?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>partial sum Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/partial-sum/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/partial-sum/</link>
	<description></description>
	<lastBuildDate>Tue, 04 Sep 2018 04:59:35 +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>partial sum Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/partial-sum/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
	</channel>
</rss>
