Implement a MyCalendarThree class to store your events. A new event can always be added.

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.

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

For each 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: MyCalendarThree cal = new MyCalendarThree(); MyCalendarThree.book(start, end)

Example 1:

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 the maximum K-booking is a 1-booking.
The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.
The remaining events cause the maximum K-booking to be only a 3-booking.
Note that the last event locally causes a 2-booking, but the answer is still 3 because
eg. [10, 20), [10, 40), and [5, 15) are still triple booked.

Note:

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

Approach #1: C++.

class MyCalendarThree {
public:
MyCalendarThree() { } int book(int start, int end) {
++books[start];
--books[end];
int count = 0;
int ant = 0;
for (auto it : books) {
count += it.second;
ant = max(ant, count);
if (it.first > end) break;
}
maxNum = max(maxNum, ant);
return maxNum;
} private:
map<int, int> books;
int maxNum = 0;
};

  

Approach #2: C++.

class MyCalendarThree {
public:
MyCalendarThree() {
books[INT_MAX] = 0;
books[INT_MIN] = 0;
maxCount = 0;
} int book(int start, int end) {
auto l = prev(books.upper_bound(start));
auto r = books.lower_bound(end); for (auto curr = l, next = curr; curr != r; curr = next) {
++next;
if (next->first > end)
books[end] = curr->second;
if (curr->first <= start && next->first > start) {
maxCount = max(maxCount, books[start] = curr->second+1);
}
else {
maxCount = max(maxCount, ++curr->second);
}
}
return maxCount;
} private:
map<int, int> books;
int maxCount;
};

  

Note:

std::prev in C++.

Approach #3: C++. [segment tree]

...........

732. My Calendar III (prev)的更多相关文章

  1. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  2. [LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 题解

    题目描述 MyCalendar主要实现一个功能就是插入指定起始结束时间的事件,对于重合的次数有要求. MyCalendar I要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...

  3. LeetCode 732. My Calendar III

    原题链接在这里:https://leetcode.com/problems/my-calendar-iii/ 题目: Implement a MyCalendarThree class to stor ...

  4. My Calendar III

    class MyCalendarThree(object): """ Implement a MyCalendarThree class to store your ev ...

  5. [LeetCode] My Calendar III 我的日历之三

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  6. [Swift]LeetCode732. 我的日程安排表 III | My Calendar III

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  7. Segment Tree-732. My Calendar III

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  8. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. 一起来学linux:进程

    简单来说,每当执行一个程序或者命令,启动事件的时候都会得到一个PID,也就是进程ID.比如用户登陆的时候就会得到一个PID.如下所示.两个用户zhf和root在登陆后分别获得PID 3212和3214 ...

  2. MLGBZ

    April cloudy, boss rainy, told me he want to kick But coming so,Formosa Heart sad , Down,down,down W ...

  3. java与js互调 调用系统播放器

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  4. DOM相关操作的案例

    1 . 模态框案例 示例 :  打开网页时有一个普通的按钮,点击当前按钮显示一个背景图,中心并弹出一个弹出框,点击X的时候会关闭当前的模态框 <!DOCTYPE html> <htm ...

  5. redis的持久化RDB与AOF

    redis 持久化  Redis是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失,为了解决这个问题,Redis提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的丢失. RDB ...

  6. ubuntu下安装redis以及redis客户端在mac下的使用

    ubuntu下安装redis http://blog.fens.me/linux-redis-install/ 此方式利用brew安装包去获取最新的rdm客户端 资源失效了 https://www.j ...

  7. 线上cpu100%问题快速定位

    问题描述:服务器上部署了多个tomcat,即垂直切分的Web站点,记忆多个Java微服务,突然收到运维的cpu异常告警. 步骤一:找到最耗cpu的进程 工具:top 方法: 执行top -c,显示进程 ...

  8. python把源代码打包成.exe文件

    1.在windows命令行把当前文件夹用cd命令切换到源代码所在文件夹. 2.输入命令:pyinstaller -w -F main.py

  9. 利用ThinkPHP做项目步骤

    ThinkPHP使用规则:约定大于配置 创建入口文件: 1.在ThinkPHP目录下创建一个入口文件index.php 2.访问入口文件的同时系统会自动把对应的应用目录文件Test创建出来 3.打开H ...

  10. UTCformat 转换UTC时间并格式化成本地时间

    /** * UTCformat 转换UTC时间并格式化成本地时间 * @param {string} utc */ UTCformat (utc) { var date = new Date(utc) ...