My Calendar III——LeetCode⑪
//原题链接https://leetcode.com/problems/my-calendar-iii/submissions/
- 题目描述
Implement a
MyCalendarThreeclass 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 numbersxsuch thatstart <= 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 integerKrepresenting the largest integer such that there exists aK-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 toMyCalendarThree.bookper test case will be at most400.
In calls toMyCalendarThree.book(start, end),startandendare integers in the range[0, 10^9]. - 思路分析
每次向时间轴里插入一个有向线段,返回此时最多的重合次数class MyCalendarThree {
TreeMap<Integer,Integer> timeline = new TreeMap<Integer,Integer>(); public MyCalendarThree() { } public int book(int start, int end) {
timeline.put(start,timeline.containsKey(start) ? timeline.get(start)+1 : 1);
timeline.put(end,timeline.containsKey(end) ? timeline.get(end)-1 :-1); int result = 0,cur = 0;
for(int i:timeline.values()){
result = Math.max(result,cur=cur+i);
} return result;
}
} /**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree obj = new MyCalendarThree();
* int param_1 = obj.book(start,end);
*/
My Calendar III——LeetCode⑪的更多相关文章
- 【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要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...
- [LeetCode] My Calendar III 我的日历之三
Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...
- LeetCode 732. My Calendar III
原题链接在这里:https://leetcode.com/problems/my-calendar-iii/ 题目: Implement a MyCalendarThree class to stor ...
- [Swift]LeetCode732. 我的日程安排表 III | My Calendar III
Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...
- House Robber III leetcode 动态规划
https://leetcode.com/submissions/detail/56095603/ 这是一道不错的DP题!自己想了好久没有清晰的思路,参看大神博客!http://siukwan.sin ...
- Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...
- My Calendar III
class MyCalendarThree(object): """ Implement a MyCalendarThree class to store your ev ...
- Segment Tree-732. My Calendar III
Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...
- 732. My Calendar III (prev)
Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...
随机推荐
- 批处理脚本(.bat)实现实时监测文件夹并执行命令 [假设有新文件则拷贝到远程文件夹内]
想到一个情景.程序实时监测文件夹情况,如果有新文件进入,分析其文件名,然后如果满足预设条件,则做相应操作.比如扫描仪扫描了文件,会将新文件保存进特定文件夹内,可以使用该程序来做处理. 在Windows ...
- 「二」nginx下载与安装
1.下载地址(开源版):https://nginx.org/en/download.html wget https://nginx.org/download/nginx-1.14.2.tar.gz 2 ...
- Redis集群(cluster模式)搭建(三主三从)
上一篇搭建了一主二从,并加入了哨兵,任何一个节点挂掉都不影响正常使用,实现了高可用.仍然存在一个问题,一主二从每个节点都存储着全部数据,随着业务庞大,数据量会超过节点容量,即便是redis可以配置清理 ...
- 基于近红外与可见光双目摄像头的活体人脸检测,文末附Demo
基于近红外与可见光双目摄像头的活体人脸检测原理 人脸活体检测(Face Anti-Spoofing)是人脸识别系统中的重要一环,它负责验证捕捉到的人脸是否为真实活体,以抵御各种伪造攻击,如彩色纸张打印 ...
- ModuleNotFoundError: No module named '_ctypes' when Python3
前言 运行 python 报错:ModuleNotFoundError: No module named '_ctypes' when using Value from module multipro ...
- PIO----创建Excel表格复杂使用
导出 @RequestMapping( name = "下载模板附件实现Model", value = {"/uploadFileModel"}, method ...
- Nginx 配置 HTTPS 完整过程
配置站点使用 https,并且将 http 重定向至 https. 1. nginx 的 ssl 模块安装 查看 nginx 是否安装 http_ssl_module 模块. $ /usr/local ...
- 【Esp32】为 idf 定制本地 Arduino 组件
在开始今天的水文前,老周先要奉劝一下国内某些嵌入式砖家和穴者,不要看不起 Arduino,它不是一种开发板,而是一种规范.Arduino 的思想是正确的,把各种开发板封装为统一的 API,让许多开源库 ...
- SSH登录方式及如何防止SSH端口被扫
ssh登录服务器的方式有三种:密码登录,公钥登录,证书登录.同时,密码登录有被破解的风险,网络上也有很多在扫描ssh端口的主机. 比如: 这里175.178.62.36是一个来自广东的服务器,17次尝 ...
- 学习unigui【20】unistringGrid
做成下面效果图: 采用unistringGrid控件. 问题: 1.不同的日期区间如何得到.如: 项目 开始时间时间 -- 终止使用时间 呼吸机 yyyy-mm-dd yyyy-mm-dd ...