<?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>paritition Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/paritition/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/paritition/</link>
	<description></description>
	<lastBuildDate>Sun, 14 Jun 2020 08:08:06 +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>paritition Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/paritition/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1478. Allocate Mailboxes</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1478-allocate-mailboxes/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1478-allocate-mailboxes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 14 Jun 2020 07:29:32 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[median]]></category>
		<category><![CDATA[paritition]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6916</guid>

					<description><![CDATA[<p>Given the array&#160;houses&#160;and an integer&#160;k. where&#160;houses[i]&#160;is the location of the ith house along a street, your task is to allocate&#160;k&#160;mailboxes in&#160;the street. Return the&#160;minimum&#160;total distance&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1478-allocate-mailboxes/">花花酱 LeetCode 1478. Allocate Mailboxes</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 the array&nbsp;<code>houses</code>&nbsp;and an integer&nbsp;<code>k</code>. where&nbsp;<code>houses[i]</code>&nbsp;is the location of the ith house along a street, your task is to allocate&nbsp;<code>k</code>&nbsp;mailboxes in&nbsp;the street.</p>



<p>Return the&nbsp;<strong>minimum</strong>&nbsp;total distance between each house and its nearest mailbox.</p>



<p>The answer is guaranteed to fit in a 32-bit signed integer.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/05/07/sample_11_1816.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> houses = [1,4,8,10,20], k = 3
<strong>Output:</strong> 5
<strong>Explanation: </strong>Allocate mailboxes in position 3, 9 and 20.
Minimum total distance from each houses to nearest mailboxes is |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5 
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/05/07/sample_2_1816.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> houses = [2,3,5,12,18], k = 2
<strong>Output:</strong> 9
<strong>Explanation: </strong>Allocate mailboxes in position 3 and 14.
Minimum total distance from each houses to nearest mailboxes is |2-3| + |3-3| + |5-3| + |12-14| + |18-14| = 9.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> houses = [7,4,6,1], k = 1
<strong>Output:</strong> 8
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> houses = [3,6,14,10], k = 4
<strong>Output:</strong> 0
</pre>



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



<ul><li><code>n == houses.length</code></li><li><code>1 &lt;= n&nbsp;&lt;= 100</code></li><li><code>1 &lt;= houses[i] &lt;= 10^4</code></li><li><code>1 &lt;= k &lt;= n</code></li><li>Array&nbsp;<code>houses</code>&nbsp;contain unique integers.</li></ul>



<h2><strong>Solution: DP</strong></h2>



<p>First, we need to sort the houses by their location.</p>



<p>This is a partitioning problem, e.g. optimal solution to partition first N houses into K groups. (allocating K mailboxes for the first N houses).</p>



<p>The key of this problem is to solve a base case, optimally allocating one mailbox for houses[i~j], The intuition is to put the mailbox in the middle location, this only works if there are only tow houses, or all the houses are evenly distributed. The correct location is the &#8220;median position&#8221; of a set of houses. For example, if the sorted locations are [1,2,3,100], the average will be 26 which costs 146 while the median is 2, and the cost becomes 100.</p>



<p>dp[i][k] := min cost to allocate k mailboxes houses[0~i].</p>



<p>base cases: </p>



<ol><li>dp[i][1] = cost(0, i), min cost to allocate one mailbox.</li><li>dp[i][k] = 0 if k &gt; i, more mailboxes than houses. // this is actually a pruning.</li></ol>



<p>transition:</p>



<p>dp[i][k] = min(dp[p][k-1] + cost(p + 1, i)) 0 &lt;= p &lt; i,</p>



<p>allocate k-1 mailboxes for houses[0~p], and allocate one for houses[p+1~i]</p>



<p>ans:</p>



<p>dp[n-1][k]</p>



<p>Time complexity: O(n^3)<br>Space complexity: O(n^2) -&gt; O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minDistance(vector&lt;int&gt;&amp; houses, int k) {
    constexpr int kInf = 1e9;
    const int n = houses.size();
    sort(begin(houses), end(houses));
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(n + 1, kInf));
    vector&lt;vector&lt;int&gt;&gt; costs(n + 1, vector&lt;int&gt;(n + 1, kInf));
    // min cost to allocate one mailbox for houses[i~j].
    auto cost = [&amp;](int i, int j) {
      int&amp; ans = costs[i][j];
      if (ans != kInf) return ans;
      const int m = i + (j - i) / 2;
      int box = 0;
      if ((j - i + 1) &amp; 1) // odd number of houses.
        box = houses[m];
      else // even number of houses.
        box = (houses[m] + houses[m + 1]) / 2;
      ans = 0;
      for (int h = i; h &lt;= j; ++h)
        ans += abs(houses[h] - box);
      return ans;
    };
    // min cost to allocate k mailboxes for houses[0~i].
    function&lt;int(int, int)&gt; solve = [&amp;](int i, int k) {
      if (k &gt; i) return 0; // more mailboxs than houses.
      if (k == 1) return cost(0, i);
      int&amp; ans = dp[i][k];
      if (ans != kInf) return ans;
      for (int p = 0; p &lt; i; ++p)
        ans = min(ans, solve(p, k - 1) + cost(p + 1, i));
      return ans;
    };
    return solve(n - 1, k);
  }
};</pre>
</div></div>



<p>O(1) time to compute cost. O(n) Time and space for pre-processing.</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
vector&lt;int&gt; sums(n + 1);
for (int i = 1; i &lt;= n; ++i)
  sums[i] = sums[i - 1] + houses[i - 1];
// min cost to allocate one mailbox for houses[i~j].
auto cost = [&amp;](int i, int j) {      
  const int m1 = (i + j) / 2;
  const int m2 = (i + j + 1) / 2;
  const int center = (houses[m1] + houses[m2]) / 2;
  return (m1 - i + 1) * center - (sums[m1 + 1] - sums[i])
       + (sums[j + 1] - sums[m2]) - (j - m2 + 1) * center;
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1478-allocate-mailboxes/">花花酱 LeetCode 1478. Allocate Mailboxes</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-1478-allocate-mailboxes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 915. Partition Array into Disjoint Intervals</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-915-partition-array-into-disjoint-intervals/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-915-partition-array-into-disjoint-intervals/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Sep 2018 14:57:56 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Greedy]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[paritition]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4097</guid>

					<description><![CDATA[<p>Problem Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element in left is less than or equal to every element in right. left and right are non-empty. left has the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-915-partition-array-into-disjoint-intervals/">花花酱 LeetCode 915. Partition Array into Disjoint Intervals</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>Given an array <code>A</code>, partition it into two (contiguous) subarrays <code>left</code> and <code>right</code> so that:</p>
<ul>
<li>Every element in <code>left</code> is less than or equal to every element in <code>right</code>.</li>
<li><code>left</code> and <code>right</code> are non-empty.</li>
<li><code>left</code> has the smallest possible size.</li>
</ul>
<p>Return the <strong>length</strong> of <code>left</code> after such a partitioning.  It is guaranteed that such a partitioning exists.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[5,0,3,8,6]</span>
<strong>Output: </strong><span id="example-output-1">3</span>
<strong>Explanation: </strong>left = [5,0,3], right = [8,6]
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre  class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[1,1,1,0,6,12]</span>
<strong>Output: </strong><span id="example-output-2">4</span>
<strong>Explanation: </strong>left = [1,1,1,0], right = [6,12]
</pre>
</div>
<p><strong>Note:</strong></p>
<ol>
<li><code>2 &lt;= A.length &lt;= 30000</code></li>
<li><code>0 &lt;= A[i] &lt;= 10^6</code></li>
<li>It is guaranteed there is at least one way to partition <code>A</code> as described.</li>
</ol>
<h1><strong>Solution 1: BST</strong></h1>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 100 ms
class Solution {
public:
  int partitionDisjoint(vector&lt;int&gt;&amp; A) {
    multiset&lt;int&gt; s(begin(A) + 1, end(A));
    int left_max = A[0];
    for (int i = 1; i &lt; A.size(); ++i) {      
      if (*begin(s) &gt;= left_max) return i;
      s.erase(s.equal_range(A[i]).first);
      left_max = max(left_max, A[i]);      
    }
    return -1;
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: Greedy</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 28 ms
class Solution {
public:
  int partitionDisjoint(vector&lt;int&gt;&amp; A) {    
    int left_max = A[0];
    int cur_max = A[0];
    int left_len = 1;
    for (int i = 1; i &lt; A.size(); ++i) {
      if (A[i] &lt; left_max) {
        left_max = cur_max;
        left_len = i + 1;        
      } else {
        cur_max = max(cur_max, A[i]);
      }
    }
    return left_len;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-915-partition-array-into-disjoint-intervals/">花花酱 LeetCode 915. Partition Array into Disjoint Intervals</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/greedy/leetcode-915-partition-array-into-disjoint-intervals/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
