<?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>intersection Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/intersection/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/intersection/</link>
	<description></description>
	<lastBuildDate>Mon, 29 Nov 2021 03:47: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>intersection Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/intersection/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 160. Intersection of Two Linked Lists</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-160-intersection-of-two-linked-lists/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-160-intersection-of-two-linked-lists/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Nov 2021 22:10:04 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[list]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8873</guid>

					<description><![CDATA[<p>Given the heads of two singly linked-lists&#160;headA&#160;and&#160;headB, return&#160;the node at which the two lists intersect. If the two linked lists have no intersection at all,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-160-intersection-of-two-linked-lists/">花花酱 LeetCode 160. Intersection of Two Linked Lists</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 heads of two singly linked-lists&nbsp;<code>headA</code>&nbsp;and&nbsp;<code>headB</code>, return&nbsp;<em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return&nbsp;<code>null</code>.</p>



<p>For example, the following two linked lists begin to intersect at node&nbsp;<code>c1</code>:</p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" alt=""/></figure>



<p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p>



<p><strong>Note</strong>&nbsp;that the linked lists must&nbsp;<strong>retain their original structure</strong>&nbsp;after the function returns.</p>



<p><strong>Custom Judge:</strong></p>



<p>The inputs to the&nbsp;<strong>judge</strong>&nbsp;are given as follows (your program is&nbsp;<strong>not</strong>&nbsp;given these inputs):</p>



<ul><li><code>intersectVal</code>&nbsp;&#8211; The value of the node where the intersection occurs. This is&nbsp;<code>0</code>&nbsp;if there is no intersected node.</li><li><code>listA</code>&nbsp;&#8211; The first linked list.</li><li><code>listB</code>&nbsp;&#8211; The second linked list.</li><li><code>skipA</code>&nbsp;&#8211; The number of nodes to skip ahead in&nbsp;<code>listA</code>&nbsp;(starting from the head) to get to the intersected node.</li><li><code>skipB</code>&nbsp;&#8211; The number of nodes to skip ahead in&nbsp;<code>listB</code>&nbsp;(starting from the head) to get to the intersected node.</li></ul>



<p>The judge will then create the linked structure based on these inputs and pass the two heads,&nbsp;<code>headA</code>&nbsp;and&nbsp;<code>headB</code>&nbsp;to your program. If you correctly return the intersected node, then your solution will be&nbsp;<strong>accepted</strong>.</p>



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



<figure class="wp-block-image;crayon:false"><img src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
<strong>Output:</strong> Intersected at '8'
<strong>Explanation:</strong> The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
</pre>



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



<figure class="wp-block-image;crayon:false"><img src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
<strong>Output:</strong> Intersected at '2'
<strong>Explanation:</strong> The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
<strong>Output:</strong> No intersection
<strong>Explanation:</strong> From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.
</pre>



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



<ul><li>The number of nodes of&nbsp;<code>listA</code>&nbsp;is in the&nbsp;<code>m</code>.</li><li>The number of nodes of&nbsp;<code>listB</code>&nbsp;is in the&nbsp;<code>n</code>.</li><li><code>0 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li><li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= skipA &lt;= m</code></li><li><code>0 &lt;= skipB &lt;= n</code></li><li><code>intersectVal</code>&nbsp;is&nbsp;<code>0</code>&nbsp;if&nbsp;<code>listA</code>&nbsp;and&nbsp;<code>listB</code>&nbsp;do not intersect.</li><li><code>intersectVal == listA[skipA] == listB[skipB]</code>&nbsp;if&nbsp;<code>listA</code>&nbsp;and&nbsp;<code>listB</code>&nbsp;intersect.</li></ul>



<p><strong>Follow up:</strong>&nbsp;Could you write a solution that runs in&nbsp;<code>O(n)</code>&nbsp;time and use only&nbsp;<code>O(1)</code>&nbsp;memory?</p>



<p></p>



<h2><strong>Solution 1: Two Passes by swapping heads</strong></h2>



<p>Time complexity: O(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:
  ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    if (!headA || !headB) return nullptr;
    ListNode* a = headA;
    ListNode* b = headB;
    while (a != b) {
      a = a ? a-&gt;next : headB;
      b = b ? b-&gt;next : headA;
    }
    return a;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-160-intersection-of-two-linked-lists/">花花酱 LeetCode 160. Intersection of Two Linked Lists</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/list/leetcode-160-intersection-of-two-linked-lists/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1272. Remove Interval</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1272-remove-interval/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1272-remove-interval/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 01 Dec 2019 04:50:58 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[interval]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5893</guid>

					<description><![CDATA[<p>Given a&#160;sorted&#160;list of disjoint&#160;intervals, each interval&#160;intervals[i] = [a, b]&#160;represents the set of real numbers&#160;x&#160;such that&#160;a &#60;= x &#60; b. We remove the intersections between any&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1272-remove-interval/">花花酱 LeetCode 1272. Remove Interval</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 a&nbsp;<strong>sorted</strong>&nbsp;list of disjoint&nbsp;<code>intervals</code>, each interval&nbsp;<code>intervals[i] = [a, b]</code>&nbsp;represents the set of real numbers&nbsp;<code>x</code>&nbsp;such that&nbsp;<code>a &lt;= x &lt; b</code>.</p>



<p>We remove the intersections between any interval in&nbsp;<code>intervals</code>&nbsp;and the interval&nbsp;<code>toBeRemoved</code>.</p>



<p>Return a&nbsp;<strong>sorted</strong>&nbsp;list of&nbsp;<code>intervals</code>&nbsp;after all such removals.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]
<strong>Output:</strong> [[0,1],[6,7]]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> intervals = [[0,5]], toBeRemoved = [2,3]
<strong>Output:</strong> [[0,2],[3,5]]
</pre>



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



<ul><li><code>1 &lt;= intervals.length &lt;= 10^4</code></li><li><code>-10^9 &lt;= intervals[i][0] &lt; intervals[i][1] &lt;= 10^9</code></li></ul>



<h2><strong>Solution: Geometry</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: 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:
  vector&lt;vector&lt;int&gt;&gt; removeInterval(vector&lt;vector&lt;int&gt;&gt;&amp; intervals, vector&lt;int&gt;&amp; r) {
    vector&lt;vector&lt;int&gt;&gt; ans;
    for (const auto&amp; i : intervals)
      // Does not intersect
      if (i[1] &lt;= r[0] || i[0] &gt;= r[1])
        ans.push_back(i);
      else {
        // i starts first
        if (i[0] &lt; r[0]) 
          ans.push_back({i[0], r[0]});
        // i ends later
        if (i[1] &gt; r[1])
          ans.push_back({r[1], i[1]});        
      }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1272-remove-interval/">花花酱 LeetCode 1272. Remove Interval</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/geometry/leetcode-1272-remove-interval/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 350. Intersection of Two Arrays II</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-350-intersection-of-two-arrays-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-350-intersection-of-two-arrays-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Jul 2018 16:50:42 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[intersection]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3137</guid>

					<description><![CDATA[<p>Problem Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-350-intersection-of-two-arrays-ii/">花花酱 LeetCode 350. Intersection of Two Arrays II</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 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, 2]</code>.</p>
<p><b>Note:</b></p>
<ul>
<li>Each element in the result should appear as many times as it shows in both arrays.</li>
<li>The result can be in any order.</li>
</ul>
<p><b>Follow up:</b></p>
<ul>
<li>What if the given array is already sorted? How would you optimize your algorithm?</li>
<li>What if <i>nums1</i>&#8216;s size is small compared to <i>nums2</i>&#8216;s size? Which algorithm is better?</li>
<li>What if elements of <i>nums2</i> are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?</li>
</ul>
<h1><strong>Solution1: Hashtable</strong></h1>
<p>Time complexity: O(m + n)</p>
<p>Space complexity: O(m)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  vector&lt;int&gt; intersect(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) {
    unordered_map&lt;int, int&gt; count;    
    vector&lt;int&gt; ans;
    for (int num : nums1)
      ++count[num];
    for (int num : nums2)
      if (count.count(num) &amp;&amp;count[num] &gt; 0) {
        --count[num];
        ans.push_back(num);
      }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-350-intersection-of-two-arrays-ii/">花花酱 LeetCode 350. Intersection of Two Arrays II</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/hashtable/leetcode-350-intersection-of-two-arrays-ii/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[<p>题目大意：求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;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-349-intersection-of-two-arrays/">花花酱 LeetCode 349. Intersection of Two Arrays</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>题目大意：求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="crayon-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="crayon-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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-349-intersection-of-two-arrays/">花花酱 LeetCode 349. Intersection of Two Arrays</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/hashtable/leetcode-349-intersection-of-two-arrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 759. Employee Free Time</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-759-employee-free-time/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-759-employee-free-time/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 09 Jan 2018 02:11:26 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[Hard]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[interval]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[sweep line]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1574</guid>

					<description><![CDATA[<p>题目大意：给你每个员工的日历，让你找出所有员工都有空的时间段。 Problem: We are given a list schedule of employees, which represents the working time for each employee. Each employee has a list of non-overlapping Intervals, and these&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-759-employee-free-time/">花花酱 LeetCode 759. Employee Free Time</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/VTgF52uGK0Y?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：给你每个员工的日历，让你找出所有员工都有空的时间段。</p>
<p><strong>Problem:</strong></p>
<p>We are given a list <code>schedule</code> of employees, which represents the working time for each employee.</p>
<p>Each employee has a list of non-overlapping <code>Intervals</code>, and these intervals are in sorted order.</p>
<p>Return the list of finite intervals representing <b>common, positive-length free time</b> for <i>all</i> employees, also in sorted order.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
Output: [[3,4]]
Explanation:
There are a total of three employees, and all common
free time intervals would be [-inf, 1], [3, 4], [10, inf].
We discard any intervals that contain inf as they aren't finite.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
Output: [[5,6],[7,9]]</pre><p>(Even though we are representing <code>Intervals</code> in the form <code>[x, y]</code>, the objects inside are <code>Intervals</code>, not lists or arrays. For example, <code>schedule[0][0].start = 1, schedule[0][0].end = 2</code>, and <code>schedule[0][0][0]</code> is not defined.)</p>
<p>Also, we wouldn&#8217;t include intervals like [5, 5] in our answer, as they have zero length.</p>
<p><b>Note:</b></p>
<ol>
<li><code>schedule</code> and <code>schedule[i]</code> are lists with lengths in range <code>[1, 50]</code>.</li>
<li><code>0 &lt;= schedule[i].start &lt; schedule[i].end &lt;= 10^8</code>.</li>
</ol>
<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"><br />
</ins></p>
<p><strong>Idea:</strong></p>
<p>Merge Intervals (virtually)</p>
<p><img class="alignnone wp-image-1580 size-full" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/759-ep154.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/759-ep154.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/759-ep154-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/759-ep154-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><strong>Solution:</strong></p>
<p>C++</p>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(n)</p>
<p>n is the total number of intervals, n &lt;= 2500</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 81 ms
class Solution {
public:
    vector&lt;Interval&gt; employeeFreeTime(vector&lt;vector&lt;Interval&gt;&gt;&amp; schedule) {
      vector&lt;Interval&gt; all;
      for (const auto intervals : schedule)
        all.insert(all.end(), intervals.begin(), intervals.end());
      std::sort(all.begin(), all.end(), 
                [](const Interval&amp; a, const Interval&amp; b){
                  return a.start &lt; b.start;
                });
      vector&lt;Interval&gt; ans;
      int end = all.front().end;
      for (const Interval&amp; busy : all) {
        if (busy.start &gt; end) 
          ans.emplace_back(end, busy.start);  
        end = max(end, busy.end);
      }
      return ans;
    }
};</pre><p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/geometry/leetcode-56-merge-intervals/">[解题报告] LeetCode 56. Merge Intervals</a></li>
<li><a href="http://zxi.mytechroad.com/blog/geometry/leetcode-57-insert-interval/">[解题报告] LeetCode 57. Insert Interval</a></li>
<li><a href="http://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-729-my-calendar-i/">[解题报告] LeetCode 729. My Calendar &#8211; 花花酱</a></li>
<li><a href="http://zxi.mytechroad.com/blog/geometry/leetcode-731-my-calendar-ii/">[解题报告] LeetCode 731. My Calendar II &#8211; 花花酱</a></li>
<li><a href="http://zxi.mytechroad.com/blog/geometry/732-my-calendar-iii/">花花酱 LeetCode 732. My Calendar III</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-759-employee-free-time/">花花酱 LeetCode 759. Employee Free Time</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/geometry/leetcode-759-employee-free-time/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
