{"id":855,"date":"2017-11-19T08:54:20","date_gmt":"2017-11-19T16:54:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=855"},"modified":"2018-04-19T08:36:40","modified_gmt":"2018-04-19T15:36:40","slug":"leetcode-729-my-calendar-i","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-729-my-calendar-i\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 729. My Calendar I"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 729. My Calendar I - \u5237\u9898\u627e\u5de5\u4f5c EP112\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/seQnf-5hlBo?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>Implement a\u00a0<code>MyCalendar<\/code>\u00a0class to store your events. A new event can be added if adding the event will not cause a double booking.<\/p>\n<p>Your class will have the 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>double booking<\/i>\u00a0happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)<\/p>\n<p>For each call to the method\u00a0<code>MyCalendar.book<\/code>, return\u00a0<code>true<\/code>\u00a0if the event can be added to the calendar successfully without causing a double booking. Otherwise, return\u00a0<code>false<\/code>\u00a0and do not add the event to the calendar.<\/p>\n<p>Your class will be called like this:\u00a0<code>MyCalendar cal = new MyCalendar();<\/code>\u00a0<code>MyCalendar.book(start, end)<\/code><\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"wrap:true lang:default decode:true \">MyCalendar();\r\nMyCalendar.book(10, 20); \/\/ returns true\r\nMyCalendar.book(15, 25); \/\/ returns false\r\nMyCalendar.book(20, 30); \/\/ returns true\r\nExplanation: \r\nThe first event can be booked.  The second can't because time 15 is already booked by another event.\r\nThe third event can be booked, as the first event takes every time less than 20, but not including 20.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>The number of calls to\u00a0<code>MyCalendar.book<\/code>\u00a0per test case will be at most\u00a0<code>1000<\/code>.<\/li>\n<li>In calls to\u00a0<code>MyCalendar.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>&nbsp;<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<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><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:\u00a0<\/strong><\/p>\n<p>Binary Search<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/729-ep112.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-877\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/729-ep112.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/729-ep112.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/729-ep112-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/729-ep112-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/729-ep112-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution1:<\/strong><\/p>\n<p>Brute Force: O(n^2)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 99 ms\r\nclass MyCalendar {\r\npublic:\r\n    MyCalendar() {}\r\n    \r\n    bool book(int start, int end) {        \r\n        for (const auto&amp; event : booked_) {\r\n            int s = event.first;\r\n            int e = event.second;          \r\n            if (max(s, start) &lt; min(e, end)) return false;\r\n        }       \r\n        booked_.emplace_back(start, end);        \r\n        return true;\r\n    }\r\nprivate:\r\n    vector&lt;pair&lt;int, int&gt;&gt; booked_;\r\n};<\/pre>\n<p><strong>Solution 2:<\/strong><\/p>\n<p>Binary Search O(nlogn)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 82 ms\r\nclass MyCalendar {\r\npublic:\r\n    MyCalendar() {}\r\n    \r\n    bool book(int start, int end) {        \r\n        auto it = booked_.lower_bound(start);\r\n        if (it != booked_.cend() &amp;&amp; it-&gt;first &lt; end)\r\n            return false;        \r\n        if (it != booked_.cbegin() &amp;&amp; (--it)-&gt;second &gt; start)\r\n            return false;        \r\n        booked_[start] = end;\r\n        return true;\r\n    }\r\nprivate:\r\n    map&lt;int, int&gt; booked_;\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 170 ms\r\nclass MyCalendar {\r\n    TreeMap&lt;Integer, Integer&gt; booked_;\r\n    public MyCalendar() {\r\n        booked_ = new TreeMap&lt;&gt;();\r\n    }\r\n    \r\n    public boolean book(int start, int end) {\r\n        Integer lb = booked_.floorKey(start);\r\n        if (lb != null &amp;&amp; booked_.get(lb) &gt; start) return false;\r\n        Integer ub = booked_.ceilingKey(start);\r\n        if (ub != null &amp;&amp; ub &lt; end) return false;\r\n\r\n        booked_.put(start, end);\r\n        return true;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\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<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/data-structure\/leetcode-715-range-module\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 715. Range Module<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-699-falling-squares\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 699. Falling Squares<\/a><\/li>\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<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Implement a\u00a0MyCalendar\u00a0class to store your events. A new event can be added if adding the event will not cause a double booking. Your class&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,164],"tags":[139],"class_list":["post-855","post","type-post","status-publish","format-standard","hentry","category-binary-search","category-medium","tag-range","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/855","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=855"}],"version-history":[{"count":11,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/855\/revisions"}],"predecessor-version":[{"id":2641,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/855\/revisions\/2641"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}