Skip to content

Commit cf94dfc

Browse files
authored
Create my-calendar-ii.cpp
1 parent 09eba3e commit cf94dfc

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

C++/my-calendar-ii.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Time: O(n^2)
2+
// Space: O(n)
3+
4+
class MyCalendarTwo {
5+
public:
6+
MyCalendarTwo() {
7+
8+
}
9+
10+
bool book(int start, int end) {
11+
for (const auto& p : overlaps_) {
12+
if (start < p.second && end > p.first) {
13+
return false;
14+
}
15+
}
16+
for (const auto& p : calendar_) {
17+
if (start < p.second && end > p.first) {
18+
overlaps_.emplace_back(max(start, p.first), min(end, p.second));
19+
}
20+
}
21+
calendar_.emplace_back(start, end);
22+
return true;
23+
}
24+
25+
private:
26+
vector<pair<int, int>> overlaps_;
27+
vector<pair<int, int>> calendar_;
28+
};
29+
30+
/**
31+
* Your MyCalendarTwo object will be instantiated and called as such:
32+
* MyCalendarTwo obj = new MyCalendarTwo();
33+
* bool param_1 = obj.book(start,end);
34+
*/

0 commit comments

Comments
 (0)