<?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>unique &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/unique/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 05 Apr 2025 02:23:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>unique &#8211; Huahua&#8217;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 368. Largest Divisible Subset</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-368-largest-divisible-subset/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-368-largest-divisible-subset/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Apr 2025 02:17:49 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[subset]]></category>
		<category><![CDATA[unique]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10266</guid>

					<description><![CDATA[也算是一道比较经典的DP题了，需要找到一个最大的子集，使得其中元素两两的余数为0。 我们假设有一个集合为{a1, a2, a3, &#8230;, an}, {a1 &#60; a2 &#60; &#8230; &#60; an) 如果 a2 % a1 = 0, a3 % a2 == 0, 那么a3&#8230;]]></description>
										<content:encoded><![CDATA[
<p>也算是一道比较经典的DP题了，需要找到一个最大的子集，使得其中元素两两的余数为0。</p>



<p>我们假设有一个集合为{a1, a2, a3, &#8230;, an}, {a1 &lt; a2 &lt; &#8230; &lt; an)</p>



<p>如果 a2 % a1 = 0,  a3 % a2 == 0, 那么a3 % a1 一定也等于0。也就是说a2是a1的倍数，a3是a2的倍数，那么a3一定也是a1的倍数。所以我们并不需要测试任意两个元素之间的余数为0，我们只需要检测a[i]是否为a[i-1]的倍数即可，如果成立，则可以确保a[i]也是a[i-2], a[i-3], &#8230; a[1]的倍数。这样就可以大大降低问题的复杂度。</p>



<p>接下来就需要dp就发挥威力了。我们将原数组排序，用dp[i]来表示以a[i]结尾（集合中的最大值），能够构建的子集的最大长度。</p>



<p>转移方程：dp[j] = max(dp[j], dp[i] + 1) if nums[j] % nums[i] = 0.</p>



<p>这表示，如果nums[j] 是 nums[i]的倍数，那么我们可以把nums[j]加入以nums[i]结尾的最大子集中，构建一个新的最大子集，长度+1。当然对于j，可能有好多个满足条件的i，我们需要找一个最大的。</p>



<p>举个例子：<br>nums = {2, 3, 4, 12}<br>以3结尾的最大子集{3}，长度为1。由于12%3==0，那么我们可以把12追加到{3} 中，构成{3,12}。<br>以4结尾的最大子集是{2,4}，长度为2。由于12%4==0，那么我们可以把12追加到{2,4} 中，构成{2,4,12} 。得到最优解。</p>



<p>这样我们只需要双重循环，枚举i，再枚举j (0 &lt;= i &lt; j)。时间复杂度：O(n^2)。空间复杂度：O(n)。</p>



<p>最后需要输出一个最大子集，如果不记录递推过程，则需要稍微判断一下。</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  vector&lt;int&gt; largestDivisibleSubset(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    sort(begin(nums), end(nums));
    // dp[i] = max size of subset ends with nums[i]
    vector&lt;int&gt; dp(n);
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        if (nums[j] % nums[i] == 0)
          dp[j] = max(dp[j], dp[i] + 1);
    int s = *max_element(begin(dp), end(dp));
    vector&lt;int&gt; ans;
    for (int i = n - 1; i &gt;= 0; --i) {
      if (dp[i] == s &amp;&amp; (ans.empty() || ans.back() % nums[i] == 0)) {
        ans.push_back(nums[i]);
        --s;
      }
    }
    return ans;
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-368-largest-divisible-subset/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2009. Minimum Number of Operations to Make Array Continuous</title>
		<link>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2009-minimum-number-of-operations-to-make-array-continuous/</link>
					<comments>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2009-minimum-number-of-operations-to-make-array-continuous/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 26 Nov 2021 20:32:13 +0000</pubDate>
				<category><![CDATA[Sliding Window]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[sliding window]]></category>
		<category><![CDATA[subarray]]></category>
		<category><![CDATA[unique]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8780</guid>

					<description><![CDATA[You are given an integer array&#160;nums. In one operation, you can replace&#160;any&#160;element in&#160;nums&#160;with&#160;any&#160;integer. nums&#160;is considered&#160;continuous&#160;if both of the following conditions are fulfilled: All elements in&#160;nums&#160;are&#160;unique.&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given an integer array&nbsp;<code>nums</code>. In one operation, you can replace&nbsp;<strong>any</strong>&nbsp;element in&nbsp;<code>nums</code>&nbsp;with&nbsp;<strong>any</strong>&nbsp;integer.</p>



<p><code>nums</code>&nbsp;is considered&nbsp;<strong>continuous</strong>&nbsp;if both of the following conditions are fulfilled:</p>



<ul class="wp-block-list"><li>All elements in&nbsp;<code>nums</code>&nbsp;are&nbsp;<strong>unique</strong>.</li><li>The difference between the&nbsp;<strong>maximum</strong>&nbsp;element and the&nbsp;<strong>minimum</strong>&nbsp;element in&nbsp;<code>nums</code>&nbsp;equals&nbsp;<code>nums.length - 1</code>.</li></ul>



<p>For example,&nbsp;<code>nums = [4, 2, 5, 3]</code>&nbsp;is&nbsp;<strong>continuous</strong>, but&nbsp;<code>nums = [1, 2, 3, 5, 6]</code>&nbsp;is&nbsp;<strong>not continuous</strong>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of operations to make&nbsp;</em><code>nums</code><strong><em>continuous</em></strong>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong>&nbsp;nums is already continuous.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong>&nbsp;One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong>&nbsp;One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Sliding Window</strong></h2>



<p>Remove duplicates and sort the numbers.<br>Try using nums[i] as the min number of the final array.<br>window [i, j), max &#8211; min &lt; n, then change the rest of array to fit into or append after the window, which takes n &#8211; (j &#8211; i) steps.<br>e.g. input = [10, 3, 1, 4, 5, 6, 6, 6, 11, 15] => sorted + unique => [1, 3, 4, 5, 6, 10, 11, 15]<br>n = 10, window = [3, 4, 5, 6, 10, 11], max = 11, min = 3, max &#8211; min = 8 &lt; 10<br>Final array = [3, 4, 5, 6, <strong><span class="has-inline-color has-vivid-red-color">1->7, 6<sub>2</sub>->8, 6<sub>3</sub>->9</span></strong>, 10, 11, <strong><span class="has-inline-color has-vivid-red-color">15->12</span></strong>] <br>Time complexity: O(n)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int minOperations(vector&lt;int&gt;&amp; A) {
    const int n = A.size();
    sort(begin(A), end(A));
    A.erase(unique(begin(A), end(A)), end(A));    
    int ans = INT_MAX;
    for (int i = 0, j = 0, m = A.size(); i &lt; m; ++i) {
      while (j &lt; m &amp;&amp; A[j] &lt; A[i] + n) ++j;
      ans = min(ans, n - (j - i));
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2009-minimum-number-of-operations-to-make-array-continuous/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1748. Sum of Unique Elements</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1748-sum-of-unique-elements/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1748-sum-of-unique-elements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 06 Feb 2021 16:20:22 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[unique]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8062</guid>

					<description><![CDATA[You are given an integer array&#160;nums. The unique elements of an array are the elements that appear&#160;exactly once&#160;in the array. Return&#160;the&#160;sum&#160;of all the unique elements&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given an integer array&nbsp;<code>nums</code>. The unique elements of an array are the elements that appear&nbsp;<strong>exactly once</strong>&nbsp;in the array.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>sum</strong>&nbsp;of all the unique elements of&nbsp;</em><code>nums</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The unique elements are [1,3], and the sum is 4.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,1,1,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no unique elements, and the sum is 0.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 15
<strong>Explanation:</strong> The unique elements are [1,2,3,4,5], and the sum is 15.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 100</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Hashtable</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(100)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int sumOfUnique(vector&lt;int&gt;&amp; nums) {
    vector&lt;int&gt; seen(101);
    int ans = 0;
    for (int x : nums)
      ++seen[x];
    for (int x : nums)
      if (seen[x] == 1) ans += x;
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-1748-sum-of-unique-elements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 945. Minimum Increment to Make Array Unique</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-945-minimum-increment-to-make-array-unique/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-945-minimum-increment-to-make-array-unique/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 30 Nov 2018 03:40:33 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[unique]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4371</guid>

					<description><![CDATA[Problem Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return the least number of moves to make every value&#8230;]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given an array of integers A, a <em>move</em> consists of choosing any <code>A[i]</code>, and incrementing it by <code>1</code>.</p>
<p>Return the least number of moves to make every value in <code>A</code> unique.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[1,2,2]</span>
<strong>Output: </strong><span id="example-output-1">1</span>
<strong>Explanation: </strong> After 1 move, the array could be [1, 2, 3].
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input: </strong><span id="example-input-2-1">[3,2,1,2,1,7]</span>
<strong>Output: </strong><span id="example-output-2">6</span>
<strong>Explanation: </strong> After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
It can be shown with 5 or less moves that it is impossible for the array to have all unique values.
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>0 &lt;= A.length &lt;= 40000</code></li>
<li><code>0 &lt;= A[i] &lt; 40000</code></li>
</ol>
<h1><strong>Solution: Greedy</strong></h1>
<p>Sort the elements, make sure A[i] &gt;= A[i-1] + 1, if not increase A[i] to A[i &#8211; 1] + 1</p>
<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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua, 56 ms
class Solution {
public:
  int minIncrementForUnique(vector&lt;int&gt;&amp; A) {
    int ans = 0;
    sort(begin(A), end(A));
    for (int i = 1; i &lt; A.size(); ++i) {
      if (A[i] &gt; A[i - 1]) continue;
      ans += (A[i - 1] - A[i]) + 1;
      A[i] = A[i - 1] + 1;
    }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="urvanov-syntax-highlighter-plain-tag"># Author: Huahua, Running time: 196 ms
class Solution(object):
  def minIncrementForUnique(self, A):
    A.sort()
    ans = 0
    for i in range(1, len(A)):
      if A[i] &gt; A[i - 1]: continue
      ans += A[i - 1] - A[i] + 1
      A[i] = A[i - 1] + 1
    return ans</pre><p>&nbsp;</p>
<p></div></div></p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-945-minimum-increment-to-make-array-unique/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 349. Intersection of Two Arrays</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-349-intersection-of-two-arrays/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-349-intersection-of-two-arrays/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 08 Mar 2018 16:20:07 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[set]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[unique]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2037</guid>

					<description><![CDATA[题目大意：求2个数组的交集。 Problem: https://leetcode.com/problems/intersection-of-two-arrays/description/ Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the&#8230;]]></description>
										<content:encoded><![CDATA[<p>题目大意：求2个数组的交集。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/intersection-of-two-arrays/description/">https://leetcode.com/problems/intersection-of-two-arrays/description/</a></p>
<p>Given two arrays, write a function to compute their intersection.</p>
<p><b>Example:</b><br />
Given <i>nums1</i> = <code>[1, 2, 2, 1]</code>, <i>nums2</i> = <code>[2, 2]</code>, return <code>[2]</code>.</p>
<p><b>Note:</b></p>
<ul>
<li>Each element in the result must be unique.</li>
<li>The result can be in any order.</li>
</ul>
<p>C++ using std::set_intersection</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  vector&lt;int&gt; intersection(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) {
    vector&lt;int&gt; intersection(max(nums1.size(), nums2.size()));
    std::sort(nums1.begin(), nums1.end());
    std::sort(nums2.begin(), nums2.end());
    // Inputs must be in ascending order
    auto it = std::set_intersection(nums1.begin(), nums1.end(), nums2.begin(), nums2.end(), intersection.begin());
    intersection.resize(it - intersection.begin());
    std::set&lt;int&gt; s(intersection.begin(), intersection.end());
    return vector&lt;int&gt;(s.begin(), s.end());
  }
};</pre><p>C++ hashtable</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
// Running time: 7 ms
class Solution {
public:
  vector&lt;int&gt; intersection(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) {
    unordered_set&lt;int&gt; m(nums1.begin(), nums1.end());
    vector&lt;int&gt; ans;
    for (int num : nums2) {
      if (!m.count(num)) continue;
      ans.push_back(num);
      m.erase(num);
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-349-intersection-of-two-arrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 95. Unique Binary Search Trees II</title>
		<link>https://zxi.mytechroad.com/blog/uncategorized/leetcode-95-unique-binary-search-trees-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/uncategorized/leetcode-95-unique-binary-search-trees-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 07 Mar 2018 17:20:29 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[unique]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2015</guid>

					<description><![CDATA[Problem https://leetcode.com/problems/unique-binary-search-trees-ii/description/ Given an integer n, generate all structurally unique BST&#8217;s (binary search trees) that store values 1&#8230;n. For example, Given n = 3, your program should return all 5&#8230;]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p><a href="https://leetcode.com/problems/unique-binary-search-trees-ii/description/">https://leetcode.com/problems/unique-binary-search-trees-ii/description/</a></p>
<p>Given an integer <i>n</i>, generate all structurally unique <b>BST&#8217;s</b> (binary search trees) that store values 1&#8230;<i>n</i>.</p>
<p>For example,<br />
Given <i>n</i> = 3, your program should return all 5 unique BST&#8217;s shown below.</p><pre class="urvanov-syntax-highlighter-plain-tag">1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3</pre><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>Idea: Recursion</strong></h1>
<p>for i in 1..n: pick i as root,<br />
left subtrees can be generated in the same way for n_l = 1 &#8230; i &#8211; 1,<br />
right subtrees can be generated in the same way for n_r = i + 1, &#8230;, n<br />
def gen(s, e):<br />
return [tree(i, l, r) for l in gen(s, i &#8211; 1) for r in gen(i + 1, e) for i in range(s, e+1)</p>
<p># of trees:</p>
<p>n = 0: 1<br />
n = 1: 1<br />
n = 2: 2<br />
n = 3: 5<br />
n = 4: 14<br />
n = 5: 42<br />
n = 6: 132<br />
&#8230;<br />
Trees(n) = Trees(0)*Trees(n-1) + Trees(1)*Trees(n-2) + &#8230; + Tress(n-1)*Trees(0)</p>
<p>Time complexity: O(3^n)</p>
<p>Space complexity: O(3^n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
// Running time: 16 ms
class Solution {
public:
  vector&lt;TreeNode*&gt; generateTrees(int n) {
    if (n == 0) return {};
    const auto&amp; ans = generateTrees(1, n);
    cout &lt;&lt; ans.size() &lt;&lt; endl;
    return ans;
  }
private:
  vector&lt;TreeNode*&gt; generateTrees(int l, int r) {
    if (l &gt; r) return { nullptr };
    vector&lt;TreeNode*&gt; ans;
    for (int i = l; i &lt;= r; ++i)
      for (TreeNode* left : generateTrees(l, i - 1))
        for (TreeNode* right : generateTrees(i + 1, r)) {
          ans.push_back(new TreeNode(i));
          ans.back()-&gt;left = left;
          ans.back()-&gt;right = right;
        }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua 4 ms
class Solution {
  public List&lt;TreeNode&gt; generateTrees(int n) {
    if (n == 0) return new ArrayList&lt;TreeNode&gt;();
    return generateTrees(1, n);
  }
  
  private List&lt;TreeNode&gt; generateTrees(int l, int r) {
    List&lt;TreeNode&gt; ans = new ArrayList&lt;&gt;();
    if (l &gt; r) {      
      ans.add(null);
      return ans;
    }
    for (int i = l; i &lt;= r; ++i)
      for (TreeNode left : generateTrees(l, i - 1))
        for (TreeNode right: generateTrees(i + 1, r)) {
          TreeNode root = new TreeNode(i);
          root.left = left;
          root.right = right;
          ans.add(root);
        }          
    return ans;
  }
}</pre><p></div><h2 class="tabtitle">Python 3</h2>
<div class="tabcontent">
</p><pre class="urvanov-syntax-highlighter-plain-tag">"""
Author: Huahua
Running time: 84 ms
"""
class Solution:
  def generateTrees(self, n):
    def newTree(x, l, r): 
      t = TreeNode(x)
      t.left = l
      t.right = r
      return t
    
    def gen(s, e):      
      return [newTree(i, l, r) for i in range(s, e + 1) for l in gen(s, i - 1) for r in gen(i + 1, e)] or [None]
        
    return gen(1, n) if n &gt; 0 else []</pre><p></div></div></p>
<h1>Solution 2: DP</h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua, 3 ms
class Solution {
  public List&lt;TreeNode&gt; generateTrees(int n) {
    if (n == 0) return new ArrayList&lt;TreeNode&gt;();
    List&lt;TreeNode&gt;[] dp = new List[n + 1];
    dp[0] = new ArrayList&lt;TreeNode&gt;();
    dp[0].add(null);
    for (int i = 1; i &lt;= n; ++i) {
      dp[i] = new ArrayList&lt;TreeNode&gt;();
      for (int j = 0; j &lt; i; ++j)
        for (TreeNode left : dp[j])
          for (TreeNode right: dp[i - j - 1]) {
            TreeNode root = new TreeNode(j + 1);
            root.left = left;
            root.right = clone(right, j + 1);
            dp[i].add(root);
        }          
      }
    return dp[n];
  }
  
  private TreeNode clone(TreeNode root, int delta) {
    if (root == null) return root;
    TreeNode node = new TreeNode(root.val + delta);
    node.left = clone(root.left, delta);
    node.right = clone(root.right, delta);
    return node;
  }
}</pre><p></div></div></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/tree/leetcode-894-all-possible-full-binary-trees/">花花酱 LeetCode 894. All Possible Full Binary Trees</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/uncategorized/leetcode-95-unique-binary-search-trees-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 62. Unique Paths</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Sep 2017 00:25:07 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[paths]]></category>
		<category><![CDATA[unique]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=187</guid>

					<description><![CDATA[Problem: A robot is located at the top-left corner of a m x n grid (marked &#8216;Start&#8217; in the diagram below). The robot can only move either down or&#8230;]]></description>
										<content:encoded><![CDATA[<p><iframe title="花花酱 LeetCode 62. Unique Paths - 刷题找工作 EP45" width="500" height="375" src="https://www.youtube.com/embed/fmpP5Ll0Azc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>A robot is located at the top-left corner of a <i>m</i> x <i>n</i> grid (marked &#8216;Start&#8217; in the diagram below).</p>
<p>The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked &#8216;Finish&#8217; in the diagram below).</p>
<p>How many possible unique paths are there?</p>
<p><img decoding="async" src="https://leetcode.com/static/images/problemset/robot_maze.png" /></p>
<p>Above is a 3 x 7 grid. How many possible unique paths are there?</p>
<p><b>Note:</b> <i>m</i> and <i>n</i> will be at most 100.</p>
<p><strong>Idea:</strong></p>
<p>Dynamic Programming</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-196" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution1:</strong></p>
<p>C++</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {    
public:
    int uniquePaths(int m, int n) {
        if (m &lt; 0 || n &lt; 0) return 0;
        if (m == 1 &amp;&amp; n == 1) return 1;
        if (f_[m][n] &gt; 0) return f_[m][n];        
        int left_paths = uniquePaths(m - 1, n);
        int top_paths = uniquePaths(m, n - 1);
        f_[m][n] = left_paths + top_paths;
        return f_[m][n];
    }
private:
    unordered_map&lt;int, unordered_map&lt;int, int&gt;&gt; f_;
};</pre><p>Java</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
// Running time: 0 ms
class Solution {
  private int[][] dp;
  private int m;
  private int n;
  
  public int uniquePaths(int m, int n) {
    this.dp = new int[m][n];    
    this.m = m;
    this.n = n;
    return dfs(0, 0);
  }
  
  private int dfs(int x, int y) {
    if (x &gt; m - 1 || y &gt; n - 1)
      return 0;
    if (x == m - 1 &amp;&amp; y == n - 1)
      return 1;
    if (dp[x][y] == 0)     
      dp[x][y] = dfs(x + 1, y) + dfs(x , y + 1);
    return dp[x][y];
  } 
}</pre><p>&nbsp;</p>
<p><strong>Solution2:</strong></p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {    
public:
    int uniquePaths(int m, int n) {
        auto f = vector&lt;vector&lt;int&gt;&gt;(n + 1, vector&lt;int&gt;(m + 1, 0));
        f[1][1] = 1;
        
        for (int y = 1; y &lt;= n; ++y)
            for(int x = 1; x &lt;= m; ++x) {
                if (x == 1 &amp;&amp; y == 1) {
                    continue;
                } else {
                    f[y][x] = f[y-1][x] + f[y][x-1];
                }
            }
        
        return f[n][m];
    }
};</pre><p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
