{"id":1574,"date":"2018-01-08T18:11:26","date_gmt":"2018-01-09T02:11:26","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1574"},"modified":"2018-01-08T19:18:20","modified_gmt":"2018-01-09T03:18:20","slug":"leetcode-759-employee-free-time","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-759-employee-free-time\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 759. Employee Free Time"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 759. Employee Free Time - \u5237\u9898\u627e\u5de5\u4f5c EP 154\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/VTgF52uGK0Y?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>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u6bcf\u4e2a\u5458\u5de5\u7684\u65e5\u5386\uff0c\u8ba9\u4f60\u627e\u51fa\u6240\u6709\u5458\u5de5\u90fd\u6709\u7a7a\u7684\u65f6\u95f4\u6bb5\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>We are given a list\u00a0<code>schedule<\/code>\u00a0of employees, which represents the working time for each employee.<\/p>\n<p>Each employee has a list of non-overlapping\u00a0<code>Intervals<\/code>, and these intervals are in sorted order.<\/p>\n<p>Return the list of finite intervals representing\u00a0<b>common, positive-length free time<\/b>\u00a0for\u00a0<i>all<\/i>\u00a0employees, also in sorted order.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]\r\nOutput: [[3,4]]\r\nExplanation:\r\nThere are a total of three employees, and all common\r\nfree time intervals would be [-inf, 1], [3, 4], [10, inf].\r\nWe discard any intervals that contain inf as they aren't finite.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]\r\nOutput: [[5,6],[7,9]]\r\n<\/pre>\n<p>(Even though we are representing\u00a0<code>Intervals<\/code>\u00a0in the form\u00a0<code>[x, y]<\/code>, the objects inside are\u00a0<code>Intervals<\/code>, not lists or arrays. For example,\u00a0<code>schedule[0][0].start = 1, schedule[0][0].end = 2<\/code>, and\u00a0<code>schedule[0][0][0]<\/code>\u00a0is not defined.)<\/p>\n<p>Also, we wouldn&#8217;t include intervals like [5, 5] in our answer, as they have zero length.<\/p>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li><code>schedule<\/code>\u00a0and\u00a0<code>schedule[i]<\/code>\u00a0are lists with lengths in range\u00a0<code>[1, 50]<\/code>.<\/li>\n<li><code>0 &lt;= schedule[i].start &lt; schedule[i].end &lt;= 10^8<\/code>.<\/li>\n<\/ol>\n<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 \/>\n<\/ins><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Merge Intervals (virtually)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>n is the total number of intervals, n &lt;= 2500<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 81 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;Interval&gt; employeeFreeTime(vector&lt;vector&lt;Interval&gt;&gt;&amp; schedule) {\r\n      vector&lt;Interval&gt; all;\r\n      for (const auto intervals : schedule)\r\n        all.insert(all.end(), intervals.begin(), intervals.end());\r\n      std::sort(all.begin(), all.end(), \r\n                [](const Interval&amp; a, const Interval&amp; b){\r\n                  return a.start &lt; b.start;\r\n                });\r\n      vector&lt;Interval&gt; ans;\r\n      int end = all.front().end;\r\n      for (const Interval&amp; busy : all) {\r\n        if (busy.start &gt; end) \r\n          ans.emplace_back(end, busy.start);  \r\n        end = max(end, busy.end);\r\n      }\r\n      return ans;\r\n    }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-56-merge-intervals\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 56. Merge Intervals<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-57-insert-interval\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 57. Insert Interval<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-729-my-calendar-i\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 729. My Calendar &#8211; \u82b1\u82b1\u9171<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-731-my-calendar-ii\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 731. My Calendar II &#8211; \u82b1\u82b1\u9171<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/732-my-calendar-iii\/\">\u82b1\u82b1\u9171 LeetCode 732. My Calendar III<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u6bcf\u4e2a\u5458\u5de5\u7684\u65e5\u5386\uff0c\u8ba9\u4f60\u627e\u51fa\u6240\u6709\u5458\u5de5\u90fd\u6709\u7a7a\u7684\u65f6\u95f4\u6bb5\u3002 Problem: We are given a list\u00a0schedule\u00a0of employees, which represents the working time for each employee. Each employee has a list of non-overlapping\u00a0Intervals, and these&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127,165],"tags":[210,128,15,104],"class_list":["post-1574","post","type-post","status-publish","format-standard","hentry","category-geometry","category-hard","tag-intersection","tag-interval","tag-sorting","tag-sweep-line","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1574"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1574\/revisions"}],"predecessor-version":[{"id":1581,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1574\/revisions\/1581"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}