<?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>difference Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/difference/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/difference/</link>
	<description></description>
	<lastBuildDate>Mon, 31 Oct 2022 16:21:47 +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>difference Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/difference/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2451. Odd String Difference</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2451-odd-string-difference%ef%bf%bc/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2451-odd-string-difference%ef%bf%bc/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 31 Oct 2022 16:18:48 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[difference]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9874</guid>

					<description><![CDATA[<p>You are given an array of equal-length strings&#160;words. Assume that the length of each string is&#160;n. Each string&#160;words[i]&#160;can be converted into a&#160;difference integer array&#160;difference[i]&#160;of length&#160;n&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2451-odd-string-difference%ef%bf%bc/">花花酱 LeetCode 2451. Odd String Difference</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 an array of equal-length strings&nbsp;<code>words</code>. Assume that the length of each string is&nbsp;<code>n</code>.</p>



<p>Each string&nbsp;<code>words[i]</code>&nbsp;can be converted into a&nbsp;<strong>difference integer array</strong>&nbsp;<code>difference[i]</code>&nbsp;of length&nbsp;<code>n - 1</code>&nbsp;where&nbsp;<code>difference[i][j] = words[i][j+1] - words[i][j]</code>&nbsp;where&nbsp;<code>0 &lt;= j &lt;= n - 2</code>. Note that the difference between two letters is the difference between their&nbsp;<strong>positions</strong>&nbsp;in the alphabet i.e.&nbsp;the position of&nbsp;<code>'a'</code>&nbsp;is&nbsp;<code>0</code>,&nbsp;<code>'b'</code>&nbsp;is&nbsp;<code>1</code>, and&nbsp;<code>'z'</code>&nbsp;is&nbsp;<code>25</code>.</p>



<ul><li>For example, for the string&nbsp;<code>"acb"</code>, the difference integer array is&nbsp;<code>[2 - 0, 1 - 2] = [2, -1]</code>.</li></ul>



<p>All the strings in words have the same difference integer array,&nbsp;<strong>except one</strong>. You should find that string.</p>



<p>Return<em>&nbsp;the string in&nbsp;</em><code>words</code><em>&nbsp;that has different&nbsp;<strong>difference integer array</strong>.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["adc","wzy","abc"]
<strong>Output:</strong> "abc"
<strong>Explanation:</strong> 
- The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1].
- The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1].
- The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1]. 
The odd array out is [1, 1], so we return the corresponding string, "abc".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["aaa","bob","ccc","ddd"]
<strong>Output:</strong> "bob"
<strong>Explanation:</strong> All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
</pre>



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



<ul><li><code>3 &lt;= words.length &lt;= 100</code></li><li><code>n == words[i].length</code></li><li><code>2 &lt;= n &lt;= 20</code></li><li><code>words[i]</code>&nbsp;consists of lowercase English letters.</li></ul>



<h2><strong>Solution: Comparing with first string.</strong></h2>



<p>Let us pick words[0] as a reference for comparison, assuming it&#8217;s valid. If we only found one instance say words[i], that is different than words[0], we know that words[i] is bad, otherwise we should see m &#8211; 1 different words which means words[0] itself is bad.</p>



<p>Time complexity: O(m*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:
  string oddString(vector&lt;string&gt;&amp; words) {
    const int m = words.size();
    const int n = words[0].size();
    int count = 0;
    int bad = 0;
    for (int i = 1; i &lt; m; ++i) 
      for (int j = 1; j &lt; n; ++j) {
        if (words[i][j] - words[i][j - 1] 
           != words[0][j] - words[0][j - 1]) {
          ++count;
          bad = i;
          break;
        }
      }
    return words[count == 1 ? bad : 0];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2451-odd-string-difference%ef%bf%bc/">花花酱 LeetCode 2451. Odd String Difference</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/string/leetcode-2451-odd-string-difference%ef%bf%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 956. Tallest Billboard</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-956-tallest-billboard/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-956-tallest-billboard/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 10 Dec 2018 01:14:18 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[difference]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[pair]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4423</guid>

					<description><![CDATA[<p>Problem You are installing a billboard and want it to have the largest height.  The billboard will have two steel supports, one on each side. &#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-956-tallest-billboard/">花花酱 LeetCode 956. Tallest Billboard</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/iPRWkifQgoo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
<p><iframe width="500" height="375" src="https://www.youtube.com/embed/WqLslW2sFxU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>You are installing a billboard and want it to have the largest height.  The billboard will have two steel supports, one on each side.  Each steel support must be an equal height.</p>
<p>You have a collection of <code>rods</code> which can be welded together.  For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.</p>
<p>Return the largest possible height of your billboard installation.  If you cannot support the billboard, return 0.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[1,2,3,6]</span>
<strong>Output: </strong><span id="example-output-1">6</span>
<strong>Explanation:</strong> We have two disjoint subsets {1,2,3} and {6}, which have the same sum = 6.
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[1,2,3,4,5,6]</span>
<strong>Output: </strong><span id="example-output-2">10</span>
<strong>Explanation:</strong> We have two disjoint subsets {2,3,5} and {4,6}, which have the same sum = 10.
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">[1,2]</span>
<strong>Output: </strong><span id="example-output-3">0</span>
<strong>Explanation: </strong>The billboard cannot be supported, so we return 0.
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>0 &lt;= rods.length &lt;= 20</code></li>
<li><code>1 &lt;= rods[i] &lt;= 1000</code></li>
<li><code>The sum of rods is at most 5000.</code></li>
</ol>
<h1><strong>Solution: DP</strong></h1>
<p>如果直接<strong>暴力搜索</strong>的话时间复杂度是O(3^N)，铁定超时。对于每一根我们可以选择1、放到左边，2、放到右边，3、不使用。最后再看一下左边和右边是否相同。</p>
<p>题目的数据规模中的这句话非常重要：</p>
<p><code>The sum of rods is at most 5000.</code></p>
<p>这句话就是告诉你算法的时间复杂度和sum of rods有关系，通常需要使用DP。</p>
<p>由于每根柱子只能使用一次（让我们想到了 回复 <strong>01背包</strong>），但是我们怎么去描述放到左边还是放到右边呢？</p>
<p>Naive的方法是用 dp[i] 表示使用前i个柱子能够构成的柱子高度的集合。</p>
<p>e.g. dp[i] = {(h1, h2)},  h1 &lt;= h2</p>
<p>和暴力搜索比起来DP已经对状态进行了压缩，因为我并不需要关心h1, h2是通过哪些（在我之前的）柱子构成了，我只关心它们的当前高度。</p>
<p>然后我可以选择</p>
<p>1、不用第i根柱子</p>
<p>2、放到低的那一堆</p>
<p>3、放到高的那一堆</p>
<p>状态转移的伪代码：</p>
<p>for h1, h2 in dp[i &#8211; 1]:</p>
<p>dp[i] += (h1, h2)        # not used</p>
<p>dp[i] += (h1, h2 + h)  # put on higher</p>
<p>if h1 + h &lt; h2:</p>
<p>dp[i] += (h1 + h, h2)  # put on lower</p>
<p>else:</p>
<p>dp[i] += (h2, h1 + h)  # put on lower</p>
<p>假设 rods=[1,1,2]</p>
<p>dp[0] = {(0,0)}</p>
<p>dp[1] = {(0,0), (0,1)}</p>
<p>dp[2] = {(0,0), (0,1), (0,2), <strong>(1,1)</strong>}</p>
<p>dp[3] = {(0,0), (0,1), (0,2), (0,3), (0,4), (1,1), (1,2), (1,3), <strong>(2,2)</strong>}</p>
<p>但是dp[i]这个集合的大小可能达到sum^2，所以还是会超时&#8230;</p>
<p>时间复杂度 O(n*sum^2)</p>
<p>空间复杂度 O(n*sum^2) 可降维至 O(sum^2)</p>
<p>革命尚未成功，同志仍需努力!</p>
<p>all pairs的cost太大，我们还需要继续压缩状态！</p>
<p><strong>重点来了</strong></p>
<p>通过观察发现，若有2个pairs：</p>
<p>(h1, h2), (h3, h4),</p>
<p>h1 &lt;= h2, h3 &lt;= h4, h1 &lt; h3, h2 &#8211; h1 = h4 &#8211; h3 即 <strong>高度差</strong> 相同</p>
<p>如果 min(h1, h2) &lt; min(h3, h4) 那么(h1, h2) 不可能产生最优解，直接舍弃。</p>
<p>因为如果后面的柱子可以构成 h4 &#8211; h3/h2 &#8211; h1 填补高度差，使得两根柱子一样高，那么答案就是 h2 和 h4。但h2 &lt; h4，所以最优解只能来自后者。</p>
<p>举个例子：我有 (1, 3) 和 (2, 4) 两个pairs，它们的高度差都是2，假设我还有一个长度为2的柱子，那么我可以构成(1+2, 3) 以及 (2+2, 4)，虽然这两个都是解。但是后者的高度要大于前者，所以前者无法构成最优解，也就没必要存下来。</p>
<p>所以，我们可以把<strong>状态压缩到高度差</strong>，<strong>对于相同的高度差，我只存h1最大的</strong>。</p>
<p>我们用 dp[i][j] 来表示使用前i个柱子，高度差为j的情况下最大的公共高度h1是多少。</p>
<p>状态转移（如下图）</p>
<p>dp[i][j] = max(dp[i][j], dp[i &#8211; 1][j])</p>
<p>dp[i][j+h] = max(dp[i][j + h], dp[i &#8211; 1][j])</p>
<p>dp[i][|j-h|] = max(dp[i][|j-h|], dp[i &#8211; 1][j] + min(j, h))</p>
<p>时间复杂度 O(nsum)</p>
<p>空间复杂度 O(nsum) 可降维至 O(sum)</p>
<p><img class="alignnone size-full wp-image-4428" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/956-ep234.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/956-ep234.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/956-ep234-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/956-ep234-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-4432" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/956-ep234-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/956-ep234-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/956-ep234-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/956-ep234-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p>dp[i] := max common height of two piles of height difference i.</p>
<p>e.g. y1 = 5, y2 = 9 =&gt; dp[9 &#8211; 5] = min(5, 9) =&gt; dp[4] = 5.</p>
<p>answer: dp[0]</p>
<p>Time complexity: O(n*Sum)</p>
<p>Space complexity: O(Sum)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++ hashmap</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 172 ms
class Solution {
public:
  int tallestBillboard(vector&lt;int&gt;&amp; rods) {
    unordered_map&lt;int, int&gt; dp;
    dp[0] = 0;
    for (int rod : rods) {      
      auto cur = dp;
      for (const auto&amp; kv : cur) {
        const int k = kv.first;
        dp[k + rod] = max(dp[k + rod], cur[k]);
        dp[abs(k - rod)] = max(dp[abs(k - rod)], cur[k] + min(rod, k));
      }    
    }
    return dp[0];
  }
};</pre><p></div><h2 class="tabtitle">C++ / array</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 8 ms
class Solution {
public:
  int tallestBillboard(vector&lt;int&gt;&amp; rods) {    
    const int s = accumulate(begin(rods), end(rods), 0);
    vector&lt;int&gt; dp(s + 1, -1);    
    dp[0] = 0;
    for (int rod : rods) {    
      vector&lt;int&gt; cur(dp);
      for (int i = 0; i &lt;= s - rod; ++i) {
        if (cur[i] &lt; 0) continue;
        dp[i + rod] = max(dp[i + rod], cur[i]);
        dp[abs(i - rod)] = max(dp[abs(i - rod)], cur[i] + min(rod, i));
      }    
    }
    return dp[0];
  }
};</pre><p></div><h2 class="tabtitle">C++/2D array</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 12 ms
class Solution {
public:
  int tallestBillboard(vector&lt;int&gt;&amp; rods) {    
    const int n = rods.size();
    const int s = accumulate(begin(rods), end(rods), 0);
    // dp[i][j] := min(h1, h2), j = abs(h1 - h2)
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(s + 1, -1));
    dp[0][0] = 0;
    for (int i = 1; i &lt;= n; ++i) {      
      int h = rods[i - 1];
      for (int j = 0; j &lt;= s - h; ++j) {
        if (dp[i - 1][j] &lt; 0) continue;
        // not used
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);  
        // put on the taller one 
        dp[i][j + h] = max(dp[i][j + h], dp[i - 1][j]); 
        // put on the shorter one
        dp[i][abs(j - h)] = max(dp[i][abs(j - h)], dp[i - 1][j] + min(h, j)); 
      }    
    }
    return dp[n][0];
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-956-tallest-billboard/">花花酱 LeetCode 956. Tallest Billboard</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-956-tallest-billboard/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
