原题链接在这里: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. Python知识点总结篇(二)

    列表 列表:一个值,包含多个字构成的序列,用[ ]括起来,[]是一个空列表,不包含任何值,类似于空字符串,负数下标表示从后边开始,-1表示列表最后一个下标,它是一种可变的数据类型,值可以添加.删除或改 ...

  2. 配置多用户SMB挂载

    在 system1 通过 SMB 共享目录 /devops ,并满足下列要求: 1.共享名为 devops 2.共享目录 devops 只能 group8.example.com 域中的客户端使用 3 ...

  3. JDK提供的并发工具类

    1.CountDownLatch await(),进入等待的状态 countDown(),计数器减一 应用场景:启动三个线程计算,需要对结果进行累加. /** * * CountDownLatch D ...

  4. Scala 系列(三)—— 流程控制语句

    一.条件表达式if Scala 中的 if/else 语法结构与 Java 中的一样,唯一不同的是,Scala 中的 if 表达式是有返回值的. object ScalaApp extends App ...

  5. 应用中有多个Spring Property PlaceHolder导致@Value只能获取到默认值

    背景 工作中负责的一套计费系统需要开发一个新通知功能,在扣费等事件触发后发送MQ,然后消费MQ发送邮件或短信通知给客户.因为有多套环境,测试时需要知道是从哪套环境发出的邮件,又不想维护多套通知模板,因 ...

  6. 【转载】JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案

    JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案 本文为转载,原文地址为:https://www.cnblogs.com/adversary/p/103 ...

  7. 设计模式-依赖倒置-Dependency Inversion Principle

    依赖倒置原则: 一般来说我们认为作为底层基础框架的逻辑是不应该依赖于上层逻辑的, 所以我们设计软件时也经常是: 需求 - 上层逻辑(直接实现需求) - 发现需要固化的逻辑 - 开发底层模块 - 然后上 ...

  8. Code Clean读书笔记

    代码整洁之道读书笔记 by fangpc 序言部分 "神在细节之中" - 建筑师路德维希 5S哲学(精益) 整理(Seiri):搞清楚事物之所在--通过恰当地命名之类的手段--至关 ...

  9. WAS更新web.xml配置文件不生效的问题

    问题及原因分析: 之前修复漏洞时,写了个过滤器配置在web.xml中,但是部署到服务器并重启后,重新扫描漏洞,还是没有解决对应问题.在确定了这种修复方案是切实可行之后分析,可能是配置的web.xml未 ...

  10. SparkStreaming+kafka Receiver模式

    1.图解 2.过程 1.使用Kafka的High Level Consumer API 实现,消费者不能自己去维护消费者offset,而且kafka也不关心数据是否丢失. 2.当向zookeeper中 ...