Skip to content

Commit aa2d2d0

Browse files
authored
Create my-calendar-i.cpp
1 parent da9c7d9 commit aa2d2d0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

C++/my-calendar-i.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class MyCalendar {
5+
public:
6+
MyCalendar() {
7+
8+
}
9+
10+
bool book(int s, int e) {
11+
auto next = books_.lower_bound(s);
12+
if (next != books_.end() && next->first < e) {
13+
return false;
14+
}
15+
if (next != books_.begin() && s < (--next)->second) {
16+
return false;
17+
}
18+
books_[s] = e;
19+
return true;
20+
}
21+
22+
private:
23+
map<int, int> books_;
24+
25+
};
26+
27+
/**
28+
* Your MyCalendar object will be instantiated and called as such:
29+
* MyCalendar obj = new MyCalendar();
30+
* bool param_1 = obj.book(start,end);
31+
*/
32+

0 commit comments

Comments
 (0)