<?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>overlap Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/overlap/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/overlap/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Jul 2018 16:23:58 +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>overlap Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/overlap/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 836. Rectangle Overlap</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-836-rectangle-overlap/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-836-rectangle-overlap/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 19 Jul 2018 16:23:43 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[overlap]]></category>
		<category><![CDATA[rectangle]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3223</guid>

					<description><![CDATA[<p>Problem A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-836-rectangle-overlap/">花花酱 LeetCode 836. Rectangle Overlap</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>A rectangle is represented as a list <code>[x1, y1, x2, y2]</code>, where <code>(x1, y1)</code> are the coordinates of its bottom-left corner, and <code>(x2, y2)</code> are the coordinates of its top-right corner.</p>
<p>Two rectangles overlap if the area of their intersection is positive.  To be clear, two rectangles that only touch at the corner or edges do not overlap.</p>
<p>Given two (axis-aligned) rectangles, return whether they overlap.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>rec1 = [0,0,2,2], rec2 = [1,1,3,3]
<strong>Output: </strong>true
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input: </strong>rec1 = [0,0,1,1], rec2 = [1,0,2,1]
<strong>Output: </strong>false
</pre>
<p><strong>Notes:</strong></p>
<ol>
<li>Both rectangles <code>rec1</code> and <code>rec2</code> are lists of 4 integers.</li>
<li>All coordinates in rectangles will be between <code>-10^9 </code>and<code> 10^9</code>.</li>
</ol>
<h1><strong>Solution: Geometry</strong></h1>
<p>Time complexity: O(1)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Solution {
public:
  bool isRectangleOverlap(vector&lt;int&gt;&amp; rec1, vector&lt;int&gt;&amp; rec2) {
    return rec1[0] &lt; rec2[2] &amp;&amp; rec2[0] &lt; rec1[2] &amp;&amp;
           rec1[1] &lt; rec2[3] &amp;&amp; rec2[1] &lt; rec1[3];
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-836-rectangle-overlap/">花花酱 LeetCode 836. Rectangle Overlap</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-836-rectangle-overlap/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 731. My Calendar II &#8211; 花花酱</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-731-my-calendar-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-731-my-calendar-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 20 Nov 2017 02:24:40 +0000</pubDate>
				<category><![CDATA[Easy]]></category>
		<category><![CDATA[Geometry]]></category>
		<category><![CDATA[overlap]]></category>
		<category><![CDATA[range]]></category>
		<category><![CDATA[sweep line]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=864</guid>

					<description><![CDATA[<p>Problem:  Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking. Your class will have&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-731-my-calendar-ii/">花花酱 LeetCode 731. My Calendar II &#8211; 花花酱</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/rRMdxFA-8G4?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem: </strong></p>
<p>Implement a <code>MyCalendarTwo</code> class to store your events. A new event can be added if adding the event will not cause a <b>triple</b> booking.</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>triple booking</i> happens when <b>three</b> events have some non-empty intersection (ie., there is some time that is common to all 3 events.)</p>
<p>For each call to the method <code>MyCalendar.book</code>, return <code>true</code> if the event can be added to the calendar successfully without causing a <b>triple</b> booking. Otherwise, return <code>false</code> and do not add the event to the calendar.</p>
<p>Your class will be called like this: <code>MyCalendar cal = new MyCalendar();</code> <code>MyCalendar.book(start, end)</code></p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(50, 60); // returns true
MyCalendar.book(10, 40); // returns true
MyCalendar.book(5, 15); // returns false
MyCalendar.book(5, 10); // returns true
MyCalendar.book(25, 55); // returns true
Explanation&lt;b&gt;:&lt;/b&gt; 
The first two events can be booked.  The third event can be double booked.
The fourth event (5, 15) can't be booked, because it would result in a triple booking.
The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked.
The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event;
the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.</pre><p><b>Note:</b></p>
<p>&nbsp;</p>
<ul>
<li>The number of calls to <code>MyCalendar.book</code> per test case will be at most <code>1000</code>.</li>
<li>In calls to <code>MyCalendar.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>Brute Force</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/731-ep113-1.png"><img class="alignnone size-full wp-image-875" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/731-ep113-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/731-ep113-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/731-ep113-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/731-ep113-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/731-ep113-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><b style="font-size: 1rem;">Solution1:</b></p>
<p>Brute Force</p>
<p>Time Complexity: O(n^2)</p>
<p>Space Complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 82 ms
class MyCalendarTwo {
public:
    MyCalendarTwo() {}
    
    bool book(int start, int end) {
        for (const auto&amp; kv : overlaps_)
            if (max(start, kv.first) &lt; min(end, kv.second)) return false;
        
        for (const auto&amp; kv : booked_) {
            const int ss = max(start, kv.first);
            const int ee = min(end, kv.second);
            if (ss &lt; ee) overlaps_.emplace_back(ss, ee);
        }
        
        booked_.emplace_back(start, end);
        return true;
    }
private:
    vector&lt;pair&lt;int, int&gt;&gt; booked_;
    vector&lt;pair&lt;int, int&gt;&gt; overlaps_;
};</pre><p>&nbsp;</p>
<p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 156 ms
class MyCalendarTwo {
    
    private List&lt;int[]&gt; booked_;
    private List&lt;int[]&gt; overlaps_;

    public MyCalendarTwo() {
        booked_ = new ArrayList&lt;&gt;();
        overlaps_ = new ArrayList&lt;&gt;();
    }
    
    public boolean book(int start, int end) {
        for (int[] range : overlaps_)
            if (Math.max(range[0], start) &lt; Math.min(range[1], end)) 
                return false;
        
        for (int[] range : booked_) {
            int ss = Math.max(range[0], start);
            int ee = Math.min(range[1], end);
            if (ss &lt; ee) overlaps_.add(new int[]{ss, ee});
        }
        
        booked_.add(new int[]{start, end});
        return true;
    }

}</pre><p>Python</p><pre class="crayon-plain-tag">"""
Author: Huahua
Runtime: 638 ms
"""
class MyCalendarTwo:

    def __init__(self):
        self.booked_ = []
        self.overlaps_ = []

    def book(self, start, end):
        for s, e in self.overlaps_:
            if start &lt; e and end &gt; s: return False
        
        for s, e in self.booked_:            
            if start &lt; e and end &gt; s: 
                self.overlaps_.append([max(start, s), min(end, e)])
        
        self.booked_.append([start, end])
        return True</pre><p>&nbsp;</p>
<p><strong>Solution 2:</strong></p>
<p>Counting</p>
<p>Time Complexity: O(n^2logn)</p>
<p>Space Complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 212 ms
class MyCalendarTwo {
public:
    MyCalendarTwo() {}
    
    bool book(int start, int end) {
        ++delta_[start];
        --delta_[end];
        int count = 0;
        for (const auto&amp; kv : delta_) {
            count += kv.second;
            if (count == 3) {
                --delta_[start];
                ++delta_[end];
                return false;
            }
            if (kv.first &gt; end) break;
        }
        return true;
    }
private:
    map&lt;int, int&gt; delta_;
};</pre><p>&nbsp;</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/data-structure/leetcode-715-range-module/">[解题报告] LeetCode 715. Range Module</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/geometry/leetcode-56-merge-intervals/">[解题报告] LeetCode 56. Merge Intervals</a></li>
<li><a href="http://zxi.mytechroad.com/blog/geometry/leetcode-699-falling-squares/">[解题报告] LeetCode 699. Falling Squares</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-731-my-calendar-ii/">花花酱 LeetCode 731. My Calendar II &#8211; 花花酱</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-731-my-calendar-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
