LeetCode 732. My Calendar III
原题链接在这里: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.
A 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.bookper test case will be at most400. - In calls to
MyCalendarThree.book(start, end),startandendare 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 I, My Calendar II.
LeetCode 732. My Calendar III的更多相关文章
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- [LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 题解
题目描述 MyCalendar主要实现一个功能就是插入指定起始结束时间的事件,对于重合的次数有要求. MyCalendar I要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...
- 732. My Calendar III (prev)
Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...
- LeetCode 731. My Calendar II
原题链接在这里:https://leetcode.com/problems/my-calendar-ii/ 题目: Implement a MyCalendarTwo class to store y ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- [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 ...
- Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)
732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...
- [LeetCode] My Calendar III 我的日历之三
Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...
随机推荐
- BJFU—214基于链式存储结构的图书信息表的创建和输出
#include<stdio.h>#include<stdlib.h>#define MAX 100 typedef struct bNode{ double no; char ...
- F1 相似查找字段存在的table
REPORT ZME12_BDC1. *REPORT demo_select_dynamic_conditions . TABLES: makt,dd02t,marc. TYPE-POOLS : AB ...
- golang 之 go-micro
在安装之前首先需要对go-micro有一定的了解 https://micro.mu/docs/cn/ go-micro中文文档 https://juejin.im/post/5cebafe6f265 ...
- ElasticSearch动态修改副本个数
创建只有 一个主分片,没有副本的小索引: PUT /my_temp_index{ "settings": { "number_of_shards" : 1, & ...
- ELK学习笔记之Logstash不停机自动重载配置文件
0x00 自动重新加载配置 为了可以自动检测配置文件的变动和自动重新加载配置文件,需要在启动的时候使用以下命令: ./bin/lagstash -f configfile.conf --config. ...
- 继承与构造函数(base关键字)
1.背景 我:虽然通过继承减少了代码冗余,但是,每一个子类的构造函数还是需要给所有属性赋值的,很麻烦的. 师:这个好办,用base就行啦. 我:贝司?还吉他呢! 师:别急,首先我们先介绍下实例化子类对 ...
- sleep方法动态打印 C语言
#include<Windows.h> #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- 阿里巴巴 Java 开发手册(三): 代码格式
1. [强制]大括号的使用约定.如果是大括号内为空,则简洁地写成{}即可,不需要换行:如果 是非空代码块则: 1) 左大括号前不换行. 2) 左大括号后换行. 3) 右大括号前换行. 4) 右大括号后 ...
- 2019 龙采科技java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.龙采科技等公司offer,岗位是Java后端开发,最终选择去了龙采科技. 面试了很多家公司,感觉大部分公司考察的点 ...
- Mac android studio真机调试步骤
Mac android studio真机调试步骤 这些步骤是在百度中查找的,当时记录下来了,过了个把月了,今天写出来,我也重温步骤. 1.找到android的SDK路径,一般是 /user/你的电脑名 ...