Implementa MyCalendarThree class to store your events. A new event can always beadded.
Yourclass will have one method, book(int start, int end). Formally, this represents a bookingon the half open interval [start, end), the range of real numbers x such that start <= x < end.
A K-booking happenswhen K events have some non-empty intersection (ie., there is sometime that is common to all K events.)
Foreach call to the method MyCalendar.book, return an integer K representing the largest integer such that there exists a K-booking in the calendar.
Your class will be called like this: MyCalendarThreecal = new MyCalendarThree(); MyCalendarThree.book(start, end)
Example1:
MyCalendarThree();
MyCalendarThree.book(10, 20); // returns 1
MyCalendarThree.book(50, 60); // returns 1
MyCalendarThree.book(10, 40); // returns 2
MyCalendarThree.book(5, 15); // returns 3
MyCalendarThree.book(5, 10); // returns 3
MyCalendarThree.book(25, 55); // returns 3
Explanation:
The first two events can be booked and are disjoint, so themaximum K-booking is a 1-booking.
The third event [10, 40) intersects the first event, and themaximum K-booking is a 2-booking.
The remaining events cause the maximum K-booking to be only a3-booking.
Note that the last event locally causes a 2-booking, but theanswer is still 3 because
eg. [10, 20), [10, 40), and [5, 15) are still triple booked.
Note:
· The number of calls to MyCalendarThree.book pertest case will be at most 400.
· In calls to MyCalendarThree.book(start, end), start and end areintegers in the range [0, 10^9].
通过这一题我学会了两点,其一是在编程之前一定要用笔在纸上画出代码的结构,其二是当对map使用map[a]=b,的时候map会自动将里面的值按照a的值大小排序,以后在做一个数组,需要不断的向里面添加值,且添加的值需要插入的时候,记得使用map,因为会自动排序
以后在做这种多个区域叠加的题目的时候记得利用这种数据结构,因为这种数据结构当从左向右遍历的时候能够得到任何一个点的重叠次数
其实这一题还有一个简单版本,其实使用map会简单很多,因为不用手动排序,会自动排序
class MyCalendarThree {
public:
MyCalendarThree(){
}
vector
intbook(int start, int end) {
intongoint = 0; int result = 0;
intstartMark=-1, endMark=-1;
for(int i = 0; i < store.size(); i++)
{
if(store[i].first < start)
{
result= max(result, (ongoint += store[i].second));
}
else
if(store[i].first==start)
{
startMark= 1;
store[i].second++;
result= max(result, (ongoint += store[i].second));
}
elseif (store[i].first == end)
{
if(startMark == -1)
{
startMark= 1;
store.insert(store.begin()+ i, pair
result= max(result, (ongoint += store[i].second));
}
else
{
endMark= 1;
store[i].second--;
result= max(result, (ongoint += store[i].second));
}
}
elseif(store[i].first>start&&store[i].first { if(startMark == -1) { startMark= 1; store.insert(store.begin()+ i, pair result= max(result, (ongoint += store[i].second)); } else { result= max(result, (ongoint += store[i].second)); } } else { if(startMark == -1) { startMark= 1; store.insert(store.begin()+ i, pair result= max(result, (ongoint += store[i].second)); } else if(endMark == -1) { endMark= 1; store.insert(store.begin()+ i, pair result= max(result, (ongoint += store[i].second)); } else { result= max(result, (ongoint += store[i].second)); } } } if(startMark == -1) { store.push_back(pair result= max(result, (ongoint += store[store.size()-1].second)); } if(endMark == -1) { store.push_back(pair result= max(result, (ongoint += store[store.size() - 1].second)); } returnresult; } };