原题链接在这里:https://leetcode.com/problems/my-calendar-i/description/

题目:

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

Your class will have the 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.

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

For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double 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(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
Explanation:
The first event can be booked. The second can't because time 15 is already booked by another event.
The third event can be booked, as the first event takes every time less than 20, but not including 20.

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].

题解:

根据开始时间二分法找到插入位置看前后是否有重叠.

这里使用TreeMap. floorKey找到<=给出start的最大key. ceilingKey找到>=给出start的最小key.

Time Complexity: book, O(logn).n是treemap的大小.

Space: O(n).

AC Java:

 class MyCalendar {
TreeMap<Integer, Integer> tm; public MyCalendar() {
tm = new TreeMap<Integer, Integer>();
} public boolean book(int start, int end) {
Integer prev = tm.floorKey(start);
Integer next = tm.ceilingKey(start); if((prev==null || tm.get(prev)<=start) && (next==null || end<=next)){
tm.put(start, end);
return true;
} return false;
}
} /**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/

跟上My Calendar IIMy Calendar III.

LeetCode My Calendar I的更多相关文章

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

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

  2. [LeetCode] My Calendar II 我的日历之二

    Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...

  3. [LeetCode] My Calendar I 我的日历之一

    Implement a MyCalendar class to store your events. A new event can be added if adding the event will ...

  4. [LeetCode] 731. My Calendar II 我的日历之二

    Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...

  5. LeetCode 732. My Calendar III

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

  6. LeetCode 731. My Calendar II

    原题链接在这里:https://leetcode.com/problems/my-calendar-ii/ 题目: Implement a MyCalendarTwo class to store y ...

  7. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

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

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

  9. 【LeetCode】729. My Calendar I 解题报告

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

随机推荐

  1. css实现一色多变化

    .pesudo{ position: absolute; top:50%; left: 50%; transform:translate(-50%,-50%); width: 120px; paddi ...

  2. C++ 知识点积累---待整理

  3. RocketMQ学习分享

    消息队列的流派 什么是 MQ Message Queue(MQ),消息队列中间件.很多人都说:MQ 通过将消息的发送和接收分离来实现应用程序的异步和解偶,这个给人的直觉是——MQ 是异步的,用来解耦的 ...

  4. 在windows操作系统中,查询端口占用和清除端口占用的程序

    一.在windows操作系统中,查询端口占用和清除端口占用的程序 提升权限后用:netstat -b或用 1.查询端口占用的进程ID 点击"开始"-->"运行&qu ...

  5. torch7框架 深度学习(1)

    前面已经安装好了torch,下面就来看看如何在torch框架上搭建深度学习模型,我一直觉得源码结合原理是机器学习最好的学习途径.所以我们从分析一个简单的案例开始吧. 参考Supervised Lear ...

  6. Highcharts 丢失值区域图;Highcharts 反转x轴与y轴;Highcharts 曲线区域图;Highcharts 区间区域图;Highcharts 使用区间和线的区域图

    Highcharts 丢失值区域图 chart 配置 将 chart 的 spacingBottom 属性设置为 30.表示图表间的间隔区间. var chart = { type: 'area', ...

  7. 001——vue.js初始安装:

    windows下安装: 1.https://nodejs.org/en/  下载安装node.js. 在cmd窗口输入node -v检查node是否安装成功. npm也随着node安装了:npm -v ...

  8. PostgreSQL资料汇总

    慢慢积累一些有用的资料: https://postgrespro.ru

  9. 基于AMBA总线的SPI协议IP核的设计与验证

    https://wenku.baidu.com/view/9542213131126edb6f1a1048.html?mark_pay_doc=2&mark_rec_page=1&ma ...

  10. cmakelist

    cmake 添加头文件目录,链接动态.静态库 罗列一下cmake常用的命令. CMake支持大写.小写.混合大小写的命令. 1. 添加头文件目录INCLUDE_DIRECTORIES 语法: incl ...