<?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>counting sort Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/counting-sort/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/counting-sort/</link>
	<description></description>
	<lastBuildDate>Sun, 23 Feb 2020 21:33:42 +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>counting sort Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/counting-sort/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1363. Largest Multiple of Three</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1363-largest-multiple-of-three/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1363-largest-multiple-of-three/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 23 Feb 2020 21:06:48 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[counting sort]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6376</guid>

					<description><![CDATA[<p>Given an integer array of&#160;digits,&#160;return the largest multiple of&#160;three&#160;that can be formed by concatenating some of the given digits in any order. Since the answer&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1363-largest-multiple-of-three/">花花酱 LeetCode 1363. Largest Multiple of Three</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 integer array of&nbsp;<code>digits</code>,&nbsp;return the largest multiple of&nbsp;<strong>three</strong>&nbsp;that can be formed by concatenating some of the given digits in any order.</p>



<p>Since the answer may not fit in an integer data type, return the answer as a string.</p>



<p>If there is no answer return an empty string.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> digits = [8,1,9]
<strong>Output:</strong> "981"
</pre>



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



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> digits = [1]
<strong>Output:</strong> ""
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> digits = [0,0,0,0,0,0]
<strong>Output:</strong> "0"
</pre>



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



<ul><li><code>1 &lt;= digits.length &lt;= 10^4</code></li><li><code>0 &lt;= digits[i] &lt;= 9</code></li><li>The returning answer must not contain unnecessary leading zeros.</li></ul>



<p>Solution: Greedy + Math + Counting sort</p>



<p>Count the numbers of each digit.<br>if sum % 3 == 0, we can use all digits.<br>if sum % 1 == 0, we can remove one digits among {1, 4, 7} => sum % 3 == 0<br>if sum % 2 == 0, we can remove one digits among {2, 5, 8} => sum % 3 == 0<br>if sum % 2 == 0, we have to remove two digits among {1, 4, 7} => sum % 3 == 0<br>if sum % 1 == 0, we have to remove two digits among {2, 5, 8} => sum % 3 == 0</p>



<p>Time complexity: O(n)<br>Space complexity: O(n) w/ output, O(1) w/o output</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string largestMultipleOfThree(vector&lt;int&gt;&amp; digits) {    
    vector&lt;int&gt; c(10);    
    for (int d : digits) ++c[d];
    
    auto getNum = [&amp;](const vector&lt;int&gt;&amp; ds) {
      for (int d : ds) --c[d];
      string ans;
      for (int d = 9; d &gt;= 1; --d) ans.append(c[d], '0' + d);
      ans.append(ans.empty() ? min(1, c[0]) : c[0], '0');
      return ans;
    };
    
    const int r = accumulate(begin(digits), end(digits), 0) % 3;
    vector&lt;vector&lt;int&gt;&gt; rs{{0, 3, 6, 9}, {1, 4, 7}, {2, 5, 8}};
    
    // Use all digitis.
    if (r == 0) return getNum({});
    // Remove one among {1, 4, 7}, {2, 5, 8}
    for (int d : rs[r]) if (c[d]) return getNum({d});
    // Remove two among {1, 4, 7}^2 (r = 2), {2, 5, 8}^2 (r = 1)
    for (int d1 : rs[3 - r])
      for (int d2 : rs[3 - r])
        if (d1 == d2 &amp;&amp; c[d1] &gt; 1 || d1 != d2 &amp;&amp; c[d1] &amp;&amp; c[d2])
          return getNum({d1, d2});
    return &quot;&quot;;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def largestMultipleOfThree(self, digits: List[int]) -&gt; str:
    c = collections.Counter(digits)
    def getNum(ds):
      ans = []
      for d in ds: c[d] -= 1
      for d in range(9, 0, -1): ans += [d] * c[d]
      ans += [0] * min(1 if not ans else c[0], c[0])
      return ''.join(map(str, ans))
    rs = [[0, 3, 6, 9], [1, 4, 7], [2, 5, 8]]
    r = sum(digits) % 3
    if r == 0: return getNum([])
    for d in rs[r]:
      if c[d] &gt; 0: return getNum([d])
    for d1, d2 in zip(rs[3 - r], rs[3 - r]):
      if d1 == d2 and c[d1] &gt;= 2 or d1 != d2 and c[d1] and c[d2]:
        return getNum([d1, d2])
    return &quot;&quot;</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1363-largest-multiple-of-three/">花花酱 LeetCode 1363. Largest Multiple of Three</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/math/leetcode-1363-largest-multiple-of-three/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
