Created
January 11, 2018 14:59
-
-
Save s4553711/01e15f1d2bc8953093d6dc3315aae952 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCalendar { | |
vector<pair<int, int>> books; | |
public: | |
bool book(int start, int end) { | |
for (pair<int, int> p : books) | |
if (max(p.first, start) < min(end, p.second)) return false; | |
books.push_back({start, end}); | |
return true; | |
} | |
}; | |
class MyCalendarTwo { | |
private: | |
vector<pair<int,int>> books; | |
public: | |
MyCalendarTwo() { | |
} | |
bool book(int start, int end) { | |
MyCalendar overlap; | |
for(pair<int, int> p : books) { | |
if (max(p.first, start) < min(p.second, end)) { | |
pair<int, int> overlapped = getOverlap(p.first, p.second, start, end); | |
if (!overlap.book(overlapped.first, overlapped.second)) return false; | |
} | |
} | |
books.push_back({start, end}); | |
return true; | |
} | |
pair<int, int> getOverlap(int s0, int e0, int s1, int e1) { | |
return { max(s0, s1), min(e0, e1)}; | |
} | |
}; | |
/** | |
* Your MyCalendarTwo object will be instantiated and called as such: | |
* MyCalendarTwo obj = new MyCalendarTwo(); | |
* bool param_1 = obj.book(start,end); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment