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

题目:

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

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.

triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.)

For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a triple 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(50, 60); // returns true
MyCalendar.book(10, 40); // returns true
MyCalendar.book(5, 15); // returns false
MyCalendar.book(5, 10); // returns true
MyCalendar.book(25, 55); // returns true
Explanation:
The first two events can be booked. The third event can be double booked.
The fourth event (5, 15) can't be booked, because it would result in a triple booking.
The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked.
The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event;
the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.

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

题解:

Maintian the ongoing event. For each event, at start, mark this point ongoing events count +1. At end, mark this point ongoing events count -1.

Every time when booking, first add this event, and iterate all the ongoing events, accumlating the current ongoing event counts.

If the count is larger than 2, that means there are at least 3 events overlapped. remove this event and return false.

Time Complexity: book, O(nlogn). n is tm size.

Space: O(n).

AC Java:

 class MyCalendarTwo {
TreeMap<Integer, Integer> tm; public MyCalendarTwo() {
tm = new TreeMap<>();
} public boolean book(int start, int end) {
tm.put(start, tm.getOrDefault(start, 0)+1);
tm.put(end, tm.getOrDefault(end, 0)-1);
int ongoing = 0;
for(int count : tm.values()){
ongoing += count;
if(ongoing > 2){
tm.put(start, tm.get(start)-1);
if(tm.get(start) == 0){
tm.remove(start);
} tm.put(end, tm.get(end)+1);
if(tm.get(end) == 0){
tm.remove(end);
} return false;
}
} return true;
}
} /**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo obj = new MyCalendarTwo();
* boolean param_1 = obj.book(start,end);
*/

类似My Calendar IMy Calendar III.

LeetCode 731. My Calendar II的更多相关文章

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

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

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

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

  3. [LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 题解

    题目描述 MyCalendar主要实现一个功能就是插入指定起始结束时间的事件,对于重合的次数有要求. MyCalendar I要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...

  4. 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 Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  7. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  8. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  9. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

随机推荐

  1. Delphi阿里云对象存储OSS【支持上传文件、下载文件、删除文件、创建目录、删除目录、Bucket操作等】

    作者QQ:(648437169) 点击下载➨Delphi阿里云对象存储OSS             阿里云api文档 [Delphi阿里云对象存储OSS]支持 获取Bucket列表.设置Bucket ...

  2. WPF 很少人知道的科技

    原文:WPF 很少人知道的科技 本文介绍不那么常见的 WPF 相关的知识. 本文内容 在 C# 代码中创建 DataTemplate 多个数据源合并为一个列表显示 使用附加属性做缓存,避免内存泄漏 使 ...

  3. ajax标准写法

    ajax 标准写法 $.ajax({ url:"http://www.microsoft.com", //请求的url地址 dataType:"json", / ...

  4. 在Centos7中创建.net core 项目,并用Apache做代理服务器部署.net core项目

    这一篇实例记录一次用Centos7创建并部署.net core项目的过程,希望能帮到用到的小伙伴. Kestrel 是 ASP.NET Core 项目模板中包括的默认 Web 服务器,Kestrel可 ...

  5. 学习网络BGP必备基础知识

    外部网关协议,使用TCP作为传输层协议,支持CIDR,增量更新,距离矢量路由协议,无环路,路由策略丰富,可防止路由震荡,易于扩展. BGP概述 #BGP工作原理 之 报文类型 TCP三次握手 open ...

  6. 闭锁CountDownLatch与栅栏CyclicBarrier

    https://blog.csdn.net/lmc_wy/article/details/7866863   闭锁CountDownLatch与栅栏CyclicBarrier     浅谈 java ...

  7. 又一个js乱码的秘密alert放在js文件里中文乱码,可是放在HTML里显示中文就很好

    用文本文档打开你的js文件,文件-另存为,编码更改为UTF-8保存. 回复 | PFly | 园豆:94 (初学一级) | 2017-07-17 21:32 显示结果中文乱码 支持(0)反对(0)回复 ...

  8. js中cssText批量修改元素样式

    平常编写代码,更改一个元素样式的时候,自己都是用 obj.style.width = "200px"; obj.style.position = "absolute&qu ...

  9. State Design Pattern

    注: 转载自 https://www.geeksforgeeks.org/state-design-pattern/  [以便查阅,非原创] State Design Pattern State pa ...

  10. echarts Y轴名称显示不全(转载)

    转载来源:https://blog.csdn.net/qq8241994/article/details/90720657今天在项目的开发中遇到的一个问题,echarts Y轴左侧的文字太多了,显示不 ...