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. 我的Java开发学习之旅------>使用循环递归算法把数组里数据数组合全部列出

    面试题如下:把一个数组里的数组合全部列出,比如1和2列出来为1,2,12,21. (面试题出自<Java程序员面试宝典>) 代码如下: import java.util.Arrays; i ...

  2. 我的Android进阶之旅------>真正在公司看几天代码的感触

    仅以此文来回顾这一周我的工作情况,以及由此而触发的感想. ============================================================= 来到新公司5天了, ...

  3. 【Leetcode-easy】Remove Element

    思路:遍历数组,count保存下一个元素的位置,如果不与该元素相同,那么将该数保存在count位置,并且count++,否则,继续遍历. public int removeElement(int[] ...

  4. 版本名称SNAPSHOT、alpha、beta、release、GA含义

    Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用.Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出.RC:(Release Candida ...

  5. python读取文件的几种方式

    http://www.cnblogs.com/nkwy2012/p/6023710.html

  6. 设置document.domain实现js跨域注意点

    转自:http://www.cnblogs.com/fsjohnhuang/archive/2011/12/07/2279554.html document.domain 用来得到当前网页的域名.比如 ...

  7. html5--2.5新的布局元素(4)-aside/nav

    html5--2.5新的布局元素(4)-aside/nav 学习要点 了解aside/nav元素的语义和用法 通过实例理解aside/nav元素的用法 aside元素(附属信息) aside元素通常用 ...

  8. web性能压力测试工具http_load/webbench/ad

    http_load 下载地址:http://www.acme.com/software/http_load/http_load-12mar2006.tar.gz 程序非常小,解压后也不到100K 居家 ...

  9. Centos7配置https,及多个https配置

    Centos7.2配置https,及多个https配置 1.单个https配置 检查相关依赖,如果没有就yum安装 yum install mod_ssl openssl rpm -qa| grep ...

  10. xgboost算法原理

    XGBoost是2014年3月陈天奇博士提出的,是基于CART树的一种boosting算法,XGBoost使用CART树有两点原因:对于分类问题,CART树的叶子结点对应的值是一个实际的分数,而非一个 ...