原题链接在这里: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. 记录一下我的mac的环境变量的配置参数

    #配置jdk环境export JAVA_7_HOME=/Library/java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Homeexport JAV ...

  2. 基于事件的 JavaScript 编程:异步与同

    JavaScript的优势之一是其如何处理异步代码.异步代码会被放入一个事件队列,等到所有其他代码执行后才进行,而不会阻塞线程.然而,对于初学者来说,书写异步代码可能会比较困难.而在这篇文章里,我将会 ...

  3. SQL , MERGE 简意

  4. python金融分析项目

    1.进入ipython: C:\Users\Administrator>ipython Python (v3. , ::) [MSC v. bit (AM D64)] Type 'copyrig ...

  5. Ubuntu 13.10 如何修改背景色--豆沙绿

    如何修改Ubunut 13.10的窗口背景色,在网上找到了多资料,安装了dconf,Unity Tweak Tool,GNOME Color Chooser都无法修改背景色. 最后,还是直接去修改主题 ...

  6. C++11_ tuple

    版权声明:本文为博主原创文章,未经博主允许不得转载. tuple 是一个可以装载任何变量的容器,C++11的Variadic Templates给tuple的实现带来了极大方便. tuple的实现基于 ...

  7. 浅谈title属性与alt属性

    XHTML是CSS布局的基础,webjx.com一直强调XHTML知识的学习,重视语义和文档的结构.title 和alt 属性,给我最直观的感受就是,可以提高文档的适应性,并合理提高关键词密度.在XH ...

  8. C语言编程的两个工具:valgrind和core

    检查内存泄漏: valgrind --leak-check=full ./ecox_rws_helper 来检查内存泄漏 程序崩溃看错误: ulimit -c unlimited 然后执行程序,会在当 ...

  9. c# 处理js序列化时 datetime返回UTC格式的问题

    using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...

  10. Linux:数据流重定向

    1)垃圾桶黑洞 /dev/null command > /dev/null 2)stdout与stderr写入同一个文件 command > filename >& comm ...