<?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>parition Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/parition/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/parition/</link>
	<description></description>
	<lastBuildDate>Fri, 10 Jan 2020 16:39:00 +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>parition Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/parition/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 914. X of a Kind in a Deck of Cards</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Sep 2018 08:02:20 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[gcd]]></category>
		<category><![CDATA[hashatable]]></category>
		<category><![CDATA[parition]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4087</guid>

					<description><![CDATA[<p>Problem n a deck of cards, each card has an integer written on it. Return true if and only if you can choose X &#62;= 2 such that it is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/">花花酱 LeetCode 914. X of a Kind in a Deck of Cards</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>n a deck of cards, each card has an integer written on it.</p>
<p>Return <code>true</code> if and only if you can choose <code>X &gt;= 2</code> such that it is possible to split the entire deck into 1 or more groups of cards, where:</p>
<ul>
<li>Each group has exactly <code>X</code> cards.</li>
<li>All the cards in each group have the same integer.</li>
</ul>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[1,2,3,4,4,3,2,1]</span>
<strong>Output: </strong><span id="example-output-1">true
<strong>Explanation</strong>: Possible partition [1,1],[2,2],[3,3],[4,4]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[1,1,1,2,2,2,3,3]</span>
<strong>Output: </strong><span id="example-output-2">false
</span><span id="example-output-1"><strong>Explanation</strong>: No possible partition.</span>
</pre>
<div>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">[1]</span>
<strong>Output: </strong><span id="example-output-3">false
</span><span id="example-output-1"><strong>Explanation</strong>: No possible partition.</span>
</pre>
<div>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-4-1">[1,1]</span>
<strong>Output: </strong><span id="example-output-4">true
</span><span id="example-output-1"><strong>Explanation</strong>: Possible partition [1,1]</span>
</pre>
<div>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-5-1">[1,1,2,2,2,2]</span>
<strong>Output: </strong><span id="example-output-5">true
</span><span id="example-output-1"><strong>Explanation</strong>: Possible partition [1,1],[2,2],[2,2]</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= deck.length &lt;= 10000</code></li>
<li><code>0 &lt;= deck[i] &lt; 10000</code></li>
</ol>
<h1><strong>Solution 1: HashTable + Brute Force</strong></h1>
<p>Try all possible Xs. 2 ~ deck.size()</p>
<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, 16 ms
class Solution {
public:
  bool hasGroupsSizeX(vector&lt;int&gt;&amp; deck) {
    unordered_map&lt;int, int&gt; counts;
    for (int card : deck) ++counts[card];
    for (int i = 2; i &lt;= deck.size(); ++i) {
      if (deck.size() % i) continue;
      bool ok = true;
      for (const auto&amp; pair : counts)
        if (pair.second % i) {
          ok = false;
          break;
        }
      if (ok) return true;
    }
    return false;
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: HashTable + GCD</strong></h1>
<p>Time complexity: O(nlogn)</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, 16 ms
class Solution {
public:
  bool hasGroupsSizeX(vector&lt;int&gt;&amp; deck) {
    unordered_map&lt;int, int&gt; counts;
    for (int card : deck) ++counts[card];
    int X = deck.size();
    for (const auto&amp; p : counts) 
      X = __gcd(X, p.second);
    return X &gt;= 2;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Somebody
class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;Integer, Integer&gt;();
        for (int i=0; i&lt;deck.length; i++) {
            int curr = deck[i];
            if (map.containsKey(curr)) {
                map.put(curr, map.get(curr) + 1);
            } else {
                map.put(curr, 1);
            }
        }
        
        // O(nLogn)
        int gcd = 0;
        for(Integer key: map.keySet()) {
            gcd = findGCDOfTwoNumbers(map.get(key), gcd);
        }
        
        return gcd &gt;= 2;      
    }
    
    // O(Log n)
    public static int findGCDOfTwoNumbers (int a, int b) {
        if (b == 0) {
            return a;
        }
        return findGCDOfTwoNumbers(b, a%b);
    }
}</pre><p>&nbsp;</p>
</div></div>
<p><strong> </strong></p>
</div>
</div>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-914-x-of-a-kind-in-a-deck-of-cards/">花花酱 LeetCode 914. X of a Kind in a Deck of Cards</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-914-x-of-a-kind-in-a-deck-of-cards/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 842. Split Array into Fibonacci Sequence</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-842-split-array-into-fibonacci-sequence/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-842-split-array-into-fibonacci-sequence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 27 Jul 2018 04:05:58 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[parition]]></category>
		<category><![CDATA[search]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3310</guid>

					<description><![CDATA[<p>Problem Given a string&#160;S&#160;of digits, such as&#160;S = "123456579", we can split it into a&#160;Fibonacci-like sequence&#160;[123, 456, 579]. Formally, a Fibonacci-like sequence is a list&#160;F&#160;of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-842-split-array-into-fibonacci-sequence/">花花酱 LeetCode 842. Split Array into Fibonacci Sequence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 842. Split Array into Fibonacci Sequence - 刷题找工作 EP296" width="500" height="375" src="https://www.youtube.com/embed/evQCfq5wpns?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>


<h1>Problem</h1>
<p>Given a string&nbsp;<code>S</code>&nbsp;of digits, such as&nbsp;<code>S = "123456579"</code>, we can split it into a&nbsp;<em>Fibonacci-like sequence</em>&nbsp;<code>[123, 456, 579].</code></p>
<p>Formally, a Fibonacci-like sequence is a list&nbsp;<code>F</code>&nbsp;of non-negative integers such that:</p>
<ul>
<li><code>0 &lt;= F[i] &lt;= 2^31 - 1</code>, (that is,&nbsp;each integer fits a 32-bit signed integer type);</li>
<li><code>F.length &gt;= 3</code>;</li>
<li>and<code>&nbsp;F[i] + F[i+1] = F[i+2]&nbsp;</code>for all&nbsp;<code>0 &lt;= i &lt; F.length - 2</code>.</li>
</ul>
<p>Also, note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number 0 itself.</p>
<p>Return any Fibonacci-like sequence split from&nbsp;<code>S</code>, or return&nbsp;<code>[]</code>&nbsp;if it cannot be done.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>"123456579"
<strong>Output: </strong>[123,456,579]
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>"11235813"
<strong>Output: </strong>[1,1,2,3,5,8,13]
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>"112358130"
<strong>Output: </strong>[]
<strong>Explanation: </strong>The task is impossible.
</pre>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>"0123"
<strong>Output: </strong>[]
<strong>Explanation: </strong>Leading zeroes are not allowed, so "01", "2", "3" is not valid.
</pre>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>"1101111"
<strong>Output: </strong>[110, 1, 111]
<strong>Explanation: </strong>The output [11, 0, 11, 11] would also be accepted.
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= S.length&nbsp;&lt;= 200</code></li>
<li><code>S</code>&nbsp;contains only digits.</li>
</ol>
<h1><strong>Solution: DFS</strong></h1>
<p>Time complexity: O(2^n)</p>
<p>Space complexity: O(n)</p>
<p>C++</p>
<pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms 
class Solution {
public:
  vector<int> splitIntoFibonacci(string s) {
    const int n = s.length();
    vector<int> nums;
    function<bool(int)> dfs = [&](int pos) {
      if (pos == n) return nums.size() >= 3;
      int max_len = s[pos] == '0' ? 1 : 10;
      long num = 0;
      for (int i = pos; i < min(pos + max_len, n); ++i) {
        num = num * 10 + (s[i] - '0');
        if (num > INT_MAX) break;
        if (nums.size() >= 2) {
          long sum = nums.rbegin()[0];
          sum += nums.rbegin()[1];
          if (num > sum) break;
          else if (num < sum) continue;
          // num must equaals to sum.
        }
        nums.push_back(num);
        if (dfs(i + 1)) return true;
        nums.pop_back();
      }
      return false;
    };
    dfs(0);
    return nums;
  }
};</pre>
<p>&nbsp;</p><p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-842-split-array-into-fibonacci-sequence/">花花酱 LeetCode 842. Split Array into Fibonacci Sequence</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-842-split-array-into-fibonacci-sequence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
