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

题目:

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

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.

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

For each call to the method MyCalendar.book, return an integer K representing the largest integer such that there exists a K-booking in the calendar.

Your class will be called like this: MyCalendarThree cal = new MyCalendarThree(); MyCalendarThree.book(start, end)

Example 1:

MyCalendarThree();
MyCalendarThree.book(10, 20); // returns 1
MyCalendarThree.book(50, 60); // returns 1
MyCalendarThree.book(10, 40); // returns 2
MyCalendarThree.book(5, 15); // returns 3
MyCalendarThree.book(5, 10); // returns 3
MyCalendarThree.book(25, 55); // returns 3
Explanation:
The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking.
The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.
The remaining events cause the maximum K-booking to be only a 3-booking.
Note that the last event locally causes a 2-booking, but the answer is still 3 because
eg. [10, 20), [10, 40), and [5, 15) are still triple booked.

Note:

  • The number of calls to MyCalendarThree.book per test case will be at most 400.
  • In calls to MyCalendarThree.book(start, end)start and end are integers in the range [0, 10^9].

题解:

Maintain the ongoing events by plus one at each booking start and minus one at each booling end.

Accumlate ongoing events count and return the maximum.

It could array to maintain such events, but input may be sparse. Thus it would be better to use TreeMap.

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

Space: O(n).

AC Java:

 class MyCalendarThree {
TreeMap<Integer, Integer> tm; public MyCalendarThree() {
tm = new TreeMap<>();
} public int book(int start, int end) {
tm.put(start, tm.getOrDefault(start, 0)+1);
tm.put(end, tm.getOrDefault(end, 0)-1); int res = 0;
int ongoing = 0;
for(int count : tm.values()){
ongoing += count;
res = Math.max(res, ongoing);
} return res;
}
} /**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree obj = new MyCalendarThree();
* int param_1 = obj.book(start,end);
*/

类似My Calendar IMy Calendar II.

LeetCode 732. My Calendar III的更多相关文章

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

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

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

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

  3. 732. My Calendar III (prev)

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

  4. LeetCode 731. My Calendar II

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

  5. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  6. LeetCode:组合总数III【216】

    LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...

  7. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)

    732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...

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

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

随机推荐

  1. python 之 面向对象 (异常处理)

    7.15 异常处理 1.什么是异常 异常是错误发生的信号,程序一旦出错,如果程序中还没有相应的处理机制,那么该错误就会产生一个异常抛出来,程序的运行也随之终止 2.一个异常分为三部分: 异常的追踪信息 ...

  2. CentOS7安装Grafana(Yum)

    一.概述 Grafana是一个跨平台的开源的分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知. 其特点: 丰富的可视化显示插件,包括热图.折线图.饼图,表格等等. 多数据源,支持 ...

  3. git 学习笔记 ---解决冲突

    人生不如意之事十之八九,合并分支往往也不是一帆风顺的. 准备新的feature1分支,继续我们的新分支开发: $ git checkout -b feature1 Switched to a new ...

  4. 阿里巴巴 Java 开发手册(四): OOP 规约

    . [强制]避免通过一个类的对象引用访问此类的静态变量或静态方法,无谓增加编译器解析成 本,直接用类名来访问即可. 2. [强制]所有的覆写方法,必须加@Override 注解. 说明:getObje ...

  5. C# vb .net实现颜色替换效果滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的颜色替换效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...

  6. MVC学习笔记(四)---使用linq多表联查(SQL)

    1.数据库原型(Students表中的ID和Scores表中的StudentID是对应的) 2.实现效果:查询出每个学生各个科目的成绩(用的是MVC学习笔记(三)—用EF向数据库中添加数据的架构) C ...

  7. Java自学-数组 复制数组

    Java 如何复制数组 数组的长度是不可变的,一旦分配好空间,是多长,就多长,不能增加也不能减少 步骤 1 : 复制数组 把一个数组的值,复制到另一个数组中 System.arraycopy(src, ...

  8. Python进阶(一)----函数

    Python进阶(一)----函数初识 一丶函数的初识 什么函数: ​ 函数是以功能为导向.一个函数封装一个功能 函数的优点: ​ 1.减少代码的重复性, ​ 2.增强了代码的可读性 二丶函数的结构 ...

  9. 如何删除mysql注释

    Linux命令删除注释 先把库表导出成一个.sql文件,然后使用sed命令删除注释.此种适用于mysql端口不开外网的情况. $ cat create_table.sql create table t ...

  10. python pip安装解决方法

    一招解决python pip install 安装库失败   PIP是python强大的安装利器,但是我们经常遇到安装库失败的问题,以下本人觉得最有效的解决方法: 1.打开 https://www.l ...