Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking.

Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.

triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.)

For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.

Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)

Example 1:

MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(50, 60); // returns true
MyCalendar.book(10, 40); // returns true
MyCalendar.book(5, 15); // returns false
MyCalendar.book(5, 10); // returns true
MyCalendar.book(25, 55); // returns true
Explanation:
The first two events can be booked. The third event can be double booked.
The fourth event (5, 15) can't be booked, because it would result in a triple booking.
The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked.
The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event;
the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.

Note:

  • The number of calls to MyCalendar.book per test case will be at most 1000.
  • In calls to MyCalendar.book(start, end)start and end are integers in the range [0, 10^9].
 

Approach #1: C++. [Brute Force]

class MyCalendarTwo {
public:
MyCalendarTwo() { } bool book(int start, int end) {
for (auto it : overlap) {
if (max(start, it.first) <= min(end, it.second)) return false;
} for (auto it : booked) {
int ss = max(start, it.first);
int ee = min(end, it.second); if (ss <= ee) overlap.push_back({ss, ee});
} booked.push_back({start, end});
return true;
} private:
vector<pair<int, int>> overlap;
vector<pair<int, int>> booked;
};

  

Approach #2: C++. [map].

class MyCalendarTwo {
public:
MyCalendarTwo() { } bool book(int start, int end) {
++books[start];
--books[end]; int count = 0;
for (auto it : books) {
count += it.second;
if (count >= 3) {
--books[start];
++books[end];
return false;
} if (it.first > end) break;
} return true;
} private:
map<int, int> books;
};

  

In this solution we use map{KEY} to store the time point, and using map{VALUE} to store the booking times point. In every time we travel the map inorder and count the number of booking times in this time ranges. if count >= 3 return false;

最新文章

  1. css浮雕效果
  2. UVA 10815
  3. Q3 2016 State of the Internet – Security Report
  4. 编译安装php 5.5 缺少依赖包 及解决方案
  5. git tag知多少
  6. 数据返回[数据库基础]——图解JOIN
  7. ANDROID_MARS学习笔记_S01原始版_021_MP3PLAYER001_下载mp3文件
  8. Linq 与UnitOfWork
  9. xmlns:android=&quot;http://schemas.android.com/apk/res/android的作用是
  10. 使用react native制作的微博客户端
  11. C语言复习4_while循环
  12. 数据库链接池c3p0的配置
  13. 神州数码策略路由(PBR)配置
  14. celery 定时任务
  15. jmeter之批量修改请求路径
  16. node.js中 express + multer 处理文件上传
  17. svn异常:subversion.javahl.ClientException
  18. GENA
  19. 2018/03/08 每日一学PHP 之 常量defind 和 const区别
  20. “全栈2019”Java第九十四章:局部内部类详解

热门文章

  1. 【题解】Greatest Common Increasing Subsequence
  2. js添加方法和邦定事件
  3. client , offset , scroll 系列 及百度导航栏案例
  4. ceph分布式存储系统初探
  5. jQuery+CSS3动画相册特效
  6. 20145239杜文超 《Java程序设计》第1周学习总结
  7. poj1328 Radar Installation —— 贪心
  8. html-webpack-plugin 中使用 title选项设置模版中的值无效
  9. js中的window.open返回object的错误
  10. pod lib lint 报错 Unable to find a specification for `AMap2DMap` depended upon by `DingtalkPod