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 ...
随机推荐
- Typecho实现版权声明的三种方式
在安装完Typecho之后,第一件事应该就是想着如何去折腾了.对于个人博客而言,不希望自己辛辛苦苦写的文章,被别人转载或无脑采集,还不留原地址,所以就需要在文章的末尾地方放上一个版权声明,来提醒下转载 ...
- DeepSeek 官方推出的提示词库,AI内容生成的精准导航仪!
前言 在当今数字化时代,人工智能(AI)正以前所未有的速度改变着我们的生活方式和工作模式.从简单的数据处理到复杂的创意生成,AI技术正逐渐渗透到各个领域,成为推动社会进步的重要力量.然而,如何高效地利 ...
- .NET10 - 预览版1新功能体验(一)
.NET 10 首个预览版已经在前两天发布,该版本在 .NET Runtime.SDK.libraries.C#.ASP.NET Core.Blazor 和 .NET MAUI 等多个方面都有重大改进 ...
- python 如何生成exe程序
cmd 安装包 pip install pyinstaller cd 进入程序文件夹内 3.输入命令pyinstaller -F xx.py 在dist文件夹下找到安装包即可
- Laravel11 从0开发 Swoole-Reverb 扩展包(一) - 扩展包开发
前言 大家好呀,我是yangyang.好久没更新了,最近新项目在使用laravel11(截止目前发文,laravel12也发布了)做开发,自己也是利用有些空闲时间做些除开业务以外的深入学习,因此也就萌 ...
- Linux netstat 命令查看80端口状态
Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Membershi ...
- Delphi 判断当前系统是否64位
uses Winapi.Windows; function IsWin64: Boolean; var IsWow64Process: function(Handle: THandle; var Re ...
- 【SpringCloud】SpringCloud Alibaba Nacos服务注册和配置中心
SpringCloud Alibaba Nacos服务注册和配置中心 感悟 注意:凡是cloud里面,你要开哪个组件,新加哪个注解,第一个就是启动,如@EnableFeignClients,第二个就是 ...
- explorer
explorer 是 Windows 下的一个实用命令. 实例 打开文件浏览器 explorer # 效果等同于快捷键操作 [Win + E] 使用默认浏览器打开链接 explorer "h ...
- Oracle return exit continue
常在循环体中看到下面3种语句: return exit continue 举例说明 啥都没有 -- none begin for i in 1 .. 10 loop if i < 5 then ...