<?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>ordered map Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ordered-map/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ordered-map/</link>
	<description></description>
	<lastBuildDate>Wed, 19 Sep 2018 15:33: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>ordered map Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ordered-map/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 732. My Calendar III</title>
		<link>https://zxi.mytechroad.com/blog/geometry/732-my-calendar-iii/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/732-my-calendar-iii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 07 Dec 2017 05:29:41 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[Hard]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[ordered map]]></category>
		<category><![CDATA[range]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1131</guid>

					<description><![CDATA[<p>Problem: link: https://leetcode.com/problems/my-calendar-iii/description/ Implement a MyCalendarThree class to store your events. A new event can always be added. Your class will have one method, book(int start, int end). Formally, this represents&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/732-my-calendar-iii/">花花酱 LeetCode 732. My Calendar III</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/yK9a-rT3FBQ?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>link: <a href="https://leetcode.com/problems/my-calendar-iii/description/">https://leetcode.com/problems/my-calendar-iii/description/</a></p>
<p>Implement a <code>MyCalendarThree</code> class to store your events. A new event can <b>always</b> be added.</p>
<p>Your class will have one method, <code>book(int start, int end)</code>. Formally, this represents a booking on the half open interval <code>[start, end)</code>, the range of real numbers <code>x</code> such that <code>start &lt;= x &lt; end</code>.</p>
<p>A <i>K-booking</i> happens when <b>K</b> events have some non-empty intersection (ie., there is some time that is common to all K events.)</p>
<p>For each call to the method <code>MyCalendar.book</code>, return an integer <code>K</code> representing the largest integer such that there exists a <code>K</code>-booking in the calendar.</p>
<p>Your class will be called like this: <code>MyCalendarThree cal = new MyCalendarThree();</code><code>MyCalendarThree.book(start, end)</code></p>
<p><b>Example 1:</b></p>
<pre class="crayon:false">MyCalendarThree();
MyCalendarThree.book(10, 20); // returns 1
MyCalendarThree.book(50, 60); // returns 1
MyCalendarThree.book(10, 40); // returns 2
MyCalendarThree.book(5, 15); // returns 3
MyCalendarThree.book(5, 10); // returns 3
MyCalendarThree.book(25, 55); // returns 3
Explanation<b>:</b> 
The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking.
The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.
The remaining events cause the maximum K-booking to be only a 3-booking.
Note that the last event locally causes a 2-booking, but the answer is still 3 because
eg. [10, 20), [10, 40), and [5, 15) are still triple booked.
</pre>
<p><b>Note:</b></p>
<ul>
<li>The number of calls to <code>MyCalendarThree.book</code> per test case will be at most <code>400</code>.</li>
<li>In calls to <code>MyCalendarThree.book(start, end)</code>, <code>start</code> and <code>end</code> are integers in the range <code>[0, 10^9]</code>.</li>
</ul>
<p><strong>Idea:</strong></p>
<p>Similar to <a href="http://zxi.mytechroad.com/blog/geometry/leetcode-731-my-calendar-ii/">LeetCode 731 My Calendar II</a> Use an ordered / tree map to track the number of event at current time.</p>
<p>For a new book event, increase the number of events at start, decrease the number of events at end.</p>
<p>Scan the timeline to find the maximum number of events.</p>
<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>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<h1><strong>Solution 1: Count Boundaries</strong></h1>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 116 ms
class MyCalendarThree {
public:
    MyCalendarThree() {}
    
    int book(int start, int end) {
        ++deltas_[start];
        --deltas_[end];
        int ans = 0;
        int curr = 0;
        for (const auto&amp; kv : deltas_)            
            ans = max(ans, curr += kv.second);
        return ans;
    }
private:
    map&lt;int, int&gt; deltas_;
};</pre><p></div></div></p>
<h1><strong>Solution 2</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 66 ms
class MyCalendarThree {
public:
    MyCalendarThree() {
        counts_[INT_MIN] = 0;
        counts_[INT_MAX] = 0;
        max_count_ = 0;
    }
    
    int book(int start, int end) {        
        auto l = prev(counts_.upper_bound(start));   // l-&gt;first &lt; start
        auto r = counts_.lower_bound(end);           // r-&gt;first &gt;= end
        for (auto curr = l, next = curr; curr != r; curr = next) {
            ++next;
            
            if (next-&gt;first &gt; end)
                counts_[end] = curr-&gt;second;
            
            if (curr-&gt;first &lt;= start &amp;&amp; next-&gt;first &gt; start)
                max_count_ = max(max_count_, counts_[start] = curr-&gt;second + 1);            
            else
                max_count_ = max(max_count_, ++curr-&gt;second);
        }
        
        return max_count_;
    }
private:
    map&lt;int, int&gt; counts_;
    int max_count_;
};</pre><p></div></div></p>
<h1><strong>Solution 3: Segment Tree</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 63 ms (&lt;95.65%)
class MyCalendarThree {
public:
    MyCalendarThree(): max_count_(0) {
        root_.reset(new Node(0, 100000000, 0));        
    }
    
    int book(int start, int end) {
        Add(start, end, root_.get());
        return max_count_;
    }
private:
    struct Node {
        Node(int l, int r, int count):l(l), m(-1), r(r), count(count){}
        int l;
        int m;
        int r;
        int count;
        std::unique_ptr&lt;Node&gt; left;
        std::unique_ptr&lt;Node&gt; right;
    };
    
    void Add(int start, int end, Node* root) {
        if (root-&gt;m != -1) {
            if (end &lt;= root-&gt;m) 
                Add(start, end, root-&gt;left.get());
            else if(start &gt;= root-&gt;m)
                Add(start, end, root-&gt;right.get());
            else {
                Add(start, root-&gt;m, root-&gt;left.get());
                Add(root-&gt;m, end, root-&gt;right.get());
            }
            return;
        }
        
        if (start == root-&gt;l &amp;&amp; end == root-&gt;r)
            max_count_ = max(max_count_, ++root-&gt;count);
        else if (start == root-&gt;l) {
            root-&gt;m = end;
            root-&gt;left.reset(new Node(start, root-&gt;m, root-&gt;count + 1));
            root-&gt;right.reset(new Node(root-&gt;m, root-&gt;r, root-&gt;count));
            max_count_ = max(max_count_, root-&gt;count + 1);
        } else if(end == root-&gt;r) {
            root-&gt;m = start;
            root-&gt;left.reset(new Node(root-&gt;l, root-&gt;m, root-&gt;count));
            root-&gt;right.reset(new Node(root-&gt;m, root-&gt;r, root-&gt;count + 1));
            max_count_ = max(max_count_, root-&gt;count + 1);
        } else {
            root-&gt;m = start;
            root-&gt;left.reset(new Node(root-&gt;l, root-&gt;m, root-&gt;count));
            root-&gt;right.reset(new Node(root-&gt;m, root-&gt;r, root-&gt;count));
            Add(start, end, root-&gt;right.get());
        }
    }
    
    std::unique_ptr&lt;Node&gt; root_;
    int max_count_;
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Runtime: 436 ms (&lt;88.88%)
"""
class Node:
    def __init__(self, l, r, count):
        self.l = l
        self.m = -1
        self.r = r            
        self.count = count
        self.left = None
        self.right = None
            
class MyCalendarThree:        
    def __init__(self):
        self.root = Node(0, 10**9, 0)
        self.max = 0

    def book(self, start, end):
        self.add(start, end, self.root)
        return self.max
    
    def add(self, start, end, root):
        if root.m != -1:
            if end &lt;= root.m: self.add(start, end, root.left)
            elif start &gt;= root.m: self.add(start, end, root.right)
            else:
                self.add(start, root.m, root.left)
                self.add(root.m, end, root.right)
            return
        
        if start == root.l and end == root.r:
            root.count += 1
            self.max = max(self.max, root.count)
        elif start == root.l:
            root.m = end
            root.left = Node(start, root.m, root.count + 1)
            root.right = Node(root.m, root.r, root.count)
            self.max = max(self.max, root.count + 1)
        elif end == root.r:
            root.m = start;
            root.left = Node(root.l, root.m, root.count)
            root.right = Node(root.m, root.r, root.count + 1)
            self.max = max(self.max, root.count + 1)
        else:
            root.m = start
            root.left = Node(root.l, root.m, root.count)
            root.right = Node(root.m, root.r, root.count)
            self.add(start, end, root.right)</pre><p></div></div></p>
<p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/binary-search/leetcode-729-my-calendar-i/">[解题报告] LeetCode 729. My Calendar I</a></li>
<li><a href="http://zxi.mytechroad.com/blog/geometry/leetcode-731-my-calendar-ii/">[解题报告] LeetCode 731. My Calendar II</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/732-my-calendar-iii/">花花酱 LeetCode 732. My Calendar III</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/732-my-calendar-iii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
