{"id":1131,"date":"2017-12-06T21:29:41","date_gmt":"2017-12-07T05:29:41","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1131"},"modified":"2018-09-19T08:33:47","modified_gmt":"2018-09-19T15:33:47","slug":"732-my-calendar-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/732-my-calendar-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 732. My Calendar III"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 732. My Calendar III - \u5237\u9898\u627e\u5de5\u4f5c EP126\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/yK9a-rT3FBQ?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><strong>Problem:<\/strong><\/p>\n<p>link:\u00a0<a href=\"https:\/\/leetcode.com\/problems\/my-calendar-iii\/description\/\">https:\/\/leetcode.com\/problems\/my-calendar-iii\/description\/<\/a><\/p>\n<p>Implement a\u00a0<code>MyCalendarThree<\/code>\u00a0class to store your events. A new event can\u00a0<b>always<\/b>\u00a0be added.<\/p>\n<p>Your class will have one method,\u00a0<code>book(int start, int end)<\/code>. Formally, this represents a booking on the half open interval\u00a0<code>[start, end)<\/code>, the range of real numbers\u00a0<code>x<\/code>\u00a0such that\u00a0<code>start &lt;= x &lt; end<\/code>.<\/p>\n<p>A\u00a0<i>K-booking<\/i>\u00a0happens when\u00a0<b>K<\/b>\u00a0events have some non-empty intersection (ie., there is some time that is common to all K events.)<\/p>\n<p>For each call to the method\u00a0<code>MyCalendar.book<\/code>, return an integer\u00a0<code>K<\/code>\u00a0representing the largest integer such that there exists a\u00a0<code>K<\/code>-booking in the calendar.<\/p>\n<p>Your class will be called like this:\u00a0<code>MyCalendarThree cal = new MyCalendarThree();<\/code><code>MyCalendarThree.book(start, end)<\/code><\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\">MyCalendarThree();\r\nMyCalendarThree.book(10, 20); \/\/ returns 1\r\nMyCalendarThree.book(50, 60); \/\/ returns 1\r\nMyCalendarThree.book(10, 40); \/\/ returns 2\r\nMyCalendarThree.book(5, 15); \/\/ returns 3\r\nMyCalendarThree.book(5, 10); \/\/ returns 3\r\nMyCalendarThree.book(25, 55); \/\/ returns 3\r\nExplanation<b>:<\/b> \r\nThe first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking.\r\nThe third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.\r\nThe remaining events cause the maximum K-booking to be only a 3-booking.\r\nNote that the last event locally causes a 2-booking, but the answer is still 3 because\r\neg. [10, 20), [10, 40), and [5, 15) are still triple booked.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li>The number of calls to\u00a0<code>MyCalendarThree.book<\/code>\u00a0per test case will be at most\u00a0<code>400<\/code>.<\/li>\n<li>In calls to\u00a0<code>MyCalendarThree.book(start, end)<\/code>,\u00a0<code>start<\/code>\u00a0and\u00a0<code>end<\/code>\u00a0are integers in the range\u00a0<code>[0, 10^9]<\/code>.<\/li>\n<\/ul>\n<p><strong>Idea:<\/strong><\/p>\n<p>Similar to <a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-731-my-calendar-ii\/\">LeetCode 731\u00a0My Calendar II<\/a> Use an ordered \/ tree map to track the number of event at current time.<\/p>\n<p>For a new book event, increase the number of events at start, decrease the number of events at end.<\/p>\n<p>Scan the timeline to find the maximum number of events.<\/p>\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\">\u00a0<\/ins><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4040\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4039\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4038\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-3.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/732-ep126-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution 1: Count Boundaries<\/strong><\/h1>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 116 ms\r\nclass MyCalendarThree {\r\npublic:\r\n    MyCalendarThree() {}\r\n    \r\n    int book(int start, int end) {\r\n        ++deltas_[start];\r\n        --deltas_[end];\r\n        int ans = 0;\r\n        int curr = 0;\r\n        for (const auto&amp; kv : deltas_)            \r\n            ans = max(ans, curr += kv.second);\r\n        return ans;\r\n    }\r\nprivate:\r\n    map&lt;int, int&gt; deltas_;\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 66 ms\r\nclass MyCalendarThree {\r\npublic:\r\n    MyCalendarThree() {\r\n        counts_[INT_MIN] = 0;\r\n        counts_[INT_MAX] = 0;\r\n        max_count_ = 0;\r\n    }\r\n    \r\n    int book(int start, int end) {        \r\n        auto l = prev(counts_.upper_bound(start));   \/\/ l-&gt;first &lt; start\r\n        auto r = counts_.lower_bound(end);           \/\/ r-&gt;first &gt;= end\r\n        for (auto curr = l, next = curr; curr != r; curr = next) {\r\n            ++next;\r\n            \r\n            if (next-&gt;first &gt; end)\r\n                counts_[end] = curr-&gt;second;\r\n            \r\n            if (curr-&gt;first &lt;= start &amp;&amp; next-&gt;first &gt; start)\r\n                max_count_ = max(max_count_, counts_[start] = curr-&gt;second + 1);            \r\n            else\r\n                max_count_ = max(max_count_, ++curr-&gt;second);\r\n        }\r\n        \r\n        return max_count_;\r\n    }\r\nprivate:\r\n    map&lt;int, int&gt; counts_;\r\n    int max_count_;\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 3:\u00a0Segment Tree<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 63 ms (&lt;95.65%)\r\nclass MyCalendarThree {\r\npublic:\r\n    MyCalendarThree(): max_count_(0) {\r\n        root_.reset(new Node(0, 100000000, 0));        \r\n    }\r\n    \r\n    int book(int start, int end) {\r\n        Add(start, end, root_.get());\r\n        return max_count_;\r\n    }\r\nprivate:\r\n    struct Node {\r\n        Node(int l, int r, int count):l(l), m(-1), r(r), count(count){}\r\n        int l;\r\n        int m;\r\n        int r;\r\n        int count;\r\n        std::unique_ptr&lt;Node&gt; left;\r\n        std::unique_ptr&lt;Node&gt; right;\r\n    };\r\n    \r\n    void Add(int start, int end, Node* root) {\r\n        if (root-&gt;m != -1) {\r\n            if (end &lt;= root-&gt;m) \r\n                Add(start, end, root-&gt;left.get());\r\n            else if(start &gt;= root-&gt;m)\r\n                Add(start, end, root-&gt;right.get());\r\n            else {\r\n                Add(start, root-&gt;m, root-&gt;left.get());\r\n                Add(root-&gt;m, end, root-&gt;right.get());\r\n            }\r\n            return;\r\n        }\r\n        \r\n        if (start == root-&gt;l &amp;&amp; end == root-&gt;r)\r\n            max_count_ = max(max_count_, ++root-&gt;count);\r\n        else if (start == root-&gt;l) {\r\n            root-&gt;m = end;\r\n            root-&gt;left.reset(new Node(start, root-&gt;m, root-&gt;count + 1));\r\n            root-&gt;right.reset(new Node(root-&gt;m, root-&gt;r, root-&gt;count));\r\n            max_count_ = max(max_count_, root-&gt;count + 1);\r\n        } else if(end == root-&gt;r) {\r\n            root-&gt;m = start;\r\n            root-&gt;left.reset(new Node(root-&gt;l, root-&gt;m, root-&gt;count));\r\n            root-&gt;right.reset(new Node(root-&gt;m, root-&gt;r, root-&gt;count + 1));\r\n            max_count_ = max(max_count_, root-&gt;count + 1);\r\n        } else {\r\n            root-&gt;m = start;\r\n            root-&gt;left.reset(new Node(root-&gt;l, root-&gt;m, root-&gt;count));\r\n            root-&gt;right.reset(new Node(root-&gt;m, root-&gt;r, root-&gt;count));\r\n            Add(start, end, root-&gt;right.get());\r\n        }\r\n    }\r\n    \r\n    std::unique_ptr&lt;Node&gt; root_;\r\n    int max_count_;\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRuntime: 436 ms (&lt;88.88%)\r\n\"\"\"\r\nclass Node:\r\n    def __init__(self, l, r, count):\r\n        self.l = l\r\n        self.m = -1\r\n        self.r = r            \r\n        self.count = count\r\n        self.left = None\r\n        self.right = None\r\n            \r\nclass MyCalendarThree:        \r\n    def __init__(self):\r\n        self.root = Node(0, 10**9, 0)\r\n        self.max = 0\r\n\r\n    def book(self, start, end):\r\n        self.add(start, end, self.root)\r\n        return self.max\r\n    \r\n    def add(self, start, end, root):\r\n        if root.m != -1:\r\n            if end &lt;= root.m: self.add(start, end, root.left)\r\n            elif start &gt;= root.m: self.add(start, end, root.right)\r\n            else:\r\n                self.add(start, root.m, root.left)\r\n                self.add(root.m, end, root.right)\r\n            return\r\n        \r\n        if start == root.l and end == root.r:\r\n            root.count += 1\r\n            self.max = max(self.max, root.count)\r\n        elif start == root.l:\r\n            root.m = end\r\n            root.left = Node(start, root.m, root.count + 1)\r\n            root.right = Node(root.m, root.r, root.count)\r\n            self.max = max(self.max, root.count + 1)\r\n        elif end == root.r:\r\n            root.m = start;\r\n            root.left = Node(root.l, root.m, root.count)\r\n            root.right = Node(root.m, root.r, root.count + 1)\r\n            self.max = max(self.max, root.count + 1)\r\n        else:\r\n            root.m = start\r\n            root.left = Node(root.l, root.m, root.count)\r\n            root.right = Node(root.m, root.r, root.count)\r\n            self.add(start, end, root.right)\r\n<\/pre>\n<\/div><\/div>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/binary-search\/leetcode-729-my-calendar-i\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 729. My Calendar I<\/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<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: link:\u00a0https:\/\/leetcode.com\/problems\/my-calendar-iii\/description\/ Implement a\u00a0MyCalendarThree\u00a0class to store your events. A new event can\u00a0always\u00a0be added. Your class will have one method,\u00a0book(int start, int end). Formally, this represents&#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":[284,217,183,139],"class_list":["post-1131","post","type-post","status-publish","format-standard","hentry","category-geometry","category-hard","tag-geometry","tag-hard","tag-ordered-map","tag-range","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1131","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=1131"}],"version-history":[{"count":15,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1131\/revisions"}],"predecessor-version":[{"id":1137,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1131\/revisions\/1137"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}