LeetCode My Calendar I
原题链接在这里: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.
A 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.bookper test case will be at most1000. - In calls to
MyCalendar.book(start, end),startandendare 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 II, My Calendar III.
LeetCode My Calendar I的更多相关文章
- [LeetCode] My Calendar III 我的日历之三
Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...
- [LeetCode] My Calendar II 我的日历之二
Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...
- [LeetCode] My Calendar I 我的日历之一
Implement a MyCalendar class to store your events. A new event can be added if adding the event will ...
- [LeetCode] 731. My Calendar II 我的日历之二
Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...
- LeetCode 732. My Calendar III
原题链接在这里:https://leetcode.com/problems/my-calendar-iii/ 题目: Implement a MyCalendarThree class to stor ...
- LeetCode 731. My Calendar II
原题链接在这里:https://leetcode.com/problems/my-calendar-ii/ 题目: Implement a MyCalendarTwo class to store y ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- 【LeetCode】729. My Calendar I 解题报告
[LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...
随机推荐
- 在线前端开发平台 Plunker
Plunker 网站 : http://plnkr.co/ Plunker 是一个用来创建.协作和分享 Web 开发思路的在线社区.编辑界面如下图所示: 特点: 基于 Node.js 环境运行 实时的 ...
- UART速度的问题
1 原来UART实验的,速度被设置成9600,因为UART,在计算速度的时候带的是96002 后来一次学习的时候,ADC用到串口打数据,那么他的串口速度任然是9600,原来用UBOOT中的速 度则是1 ...
- Tomcat服务部署步骤
Tomcat服务部署步骤 1. 2. 3. tar -zxvf apache-tomcat-7.0.68.tar.gz,然后修改文件夹名称为需要的名称, 使用mv命令 4. 删除 /webapps/R ...
- linux正则表达式回忆记录
好久没用linux grep相关正则表达式,现在简单记录下. grep简介 grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.通常grep有三种版本grep.egr ...
- hdu 1211 逆元
RSA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- js排序算法04——归并排序
归并排序是一种分治算法.思想是把原数组切分成较小的数组,直到每个小数组只有一个位置,再将小数组归并成较大的数组,直到最后有一个完整有序的大数组. js实现如下: function mergeSort( ...
- SQL 添加字段
制定添加在那个字段后面 ALTER TABLE `szq`.`org_sales_daily` ADD COLUMN `trade_id_onl_count` int(11) NOT NULL DEF ...
- 转载:left join和left semi join的联系和区别
1.联系 他们都是 hive join 方式的一种,join on 属于 common join(shuffle join/reduce join),而 left semi join 则属于 map ...
- http请求的GET和POST请求:查询和新增(server.php)
<?php //设置页面内容是html编码格式是utf-8 header("Content-Type: text/plain;charset=utf-8"); //heade ...
- MySQL小误区:关于set global sql_slave_skip_counter=N 命令的一些点
背景知识1: 在主从库维护中,有时候需要跳过某个无法执行的命令,需要在slave处于stop状态下,执行 set global sql_slave_skip_counter=N以跳过命令.常用 ...