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. (图解)Description Resource Path Location Type Java compiler level does not match the version of

    Description Resource Path Location Type Java compiler level does not match the version of project 编译 ...

  2. (转)JavaScript中==和===的区别

    ==   用于比较   判断 两者相等      ==在比较的时候可以转自动换数据类型 ===用于严格比较   判断两者严格相等     ===严格比较,不会进行自动转换,要求进行比较的操作数必须类型 ...

  3. php 身份证号码获取星座和生肖

    发布:thatboy   来源:Net     [大 中 小] 本文介绍下,php用身份证号码获取星座和生肖的方法,一个简单的php实例,从身份证号码中取得星座与生肖信息,有兴趣的朋友参考研究下吧.本 ...

  4. ubuntu把文件移动到指定的文件夹

    有个文件a.txt在/etc下,想把它移动到/etc/fuck文件夹中 cd /etc/fuck sudo mv a.txt 要移动到的文件夹 没报错就成功了

  5. __builtin_constant_p(x) (转帖

    本文转载自:http://blog.chinaunix.net/uid-29254195-id-3977753.html gcc的内建函数,当x为常数时返回1, x为变量时返回0. 不过这并不完全准确 ...

  6. 游戏引擎基于Handle的资源管理

    基于Handle的资源管理方案,第一时间想到的应该是Windows了,但是真正想让我实施这个方案的,是<游戏编程精粹1>里面的游戏资源管理篇章的给出的方案.在<游戏编程精粹1> ...

  7. css3立体旋转菜单

    css3立体旋转菜单,css3,3D,立体旋转,立体菜单,菜单导航,css3立体旋转菜单是一款纯css3实现的三维立体旋转导航菜单. 源码下载页:http://www.huiyi8.com/sc/71 ...

  8. IDEAL葵花宝典:java代码开发规范插件 FindBugs-IDEA

     前言: 检测代码中可能的bug及不规范的位置,检测的模式相比p3c更多,写完代码后检测下 避免低级bug,强烈建议用一下,一不小心就发现很多老代码的bug. 使用步骤: 1):打开 Settings ...

  9. Java钉钉开发_Exception_异常总结

    一.异常 1.访问ip不在白名单之中 异常信息: "errcode":60020,"errmsg":"访问ip不在白名单之中" 异常背景:若 ...

  10. 洛谷P3373线段树模板2

    题目:https://www.luogu.org/problemnew/show/P3373 带乘的线段树,更新时把加的标记也乘一下,然后取值时先乘后加. 代码如下: #include<iost ...