LeetCode 729. 我的日程安排表 I
原题链接
中等
作者:
Coderoger
,
2020-12-26 21:10:54
,
所有人可见
,
阅读 477
利用set
class MyCalendar {
public:
set<pair<int, int>> S;
MyCalendar() {
S.insert({INT_MIN, INT_MIN});
S.insert({INT_MAX, INT_MAX});
}
bool book(int start, int end) {
auto it = S.lower_bound({start, end});
auto pre = it;
it --;
if (it->second <= start && pre->first >= end) {
S.insert({start, end});
return true;
}
return false;
}
};
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar* obj = new MyCalendar();
* bool param_1 = obj->book(start,end);
*/
利用map
class MyCalendar {
public:
map<int, int> hash;
MyCalendar() {
hash[INT_MIN] = INT_MIN;
hash[INT_MAX] = INT_MAX;
}
bool book(int start, int end) {
auto it = hash.lower_bound(start);
auto pre = it;
it --;
if (pre->first >= end && it->second <= start) {
hash[start] = end;
return true;
}
return false;
}
};
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar* obj = new MyCalendar();
* bool param_1 = obj->book(start,end);
*/
我是傻逼