My Calendar III——LeetCode⑪
//原题链接https://leetcode.com/problems/my-calendar-iii/submissions/
- 题目描述
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 numbersx
such 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 integerK
representing 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.book
per test case will be at most400
.
In calls toMyCalendarThree.book(start, end)
,start
andend
are 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 ...
随机推荐
- Ubuntu详细的安装和配置ssh教程
Ubuntu安装和配置ssh的步骤如下: 打开终端,输入以下命令安装ssh: sudo apt-get install openssh-server 安装完成后,启动ssh服务: sudo syste ...
- csharp入门经典
C#简介 .NET Framework是Microsoft为开发应用程序而创建的一个具有革命意义的平台,它有运行在其他操作系统上的版本 .NET Framework的设计方式确保它可以用于各种语言,包 ...
- Opencv | 图形学 | Mingw64 | 如何正确地用MinGW64编译与配置vscode的Opencv环境
如何正确地用MinGW64编译与配置vscode的Opencv环境 1.前情提要 最近有关于图形学的授课,教授开始布置的上机打码的代码实现作业了.虽说教授为了让我们省心,直接就整了个环境已经配置好的几 ...
- 面试题30. 包含min函数的栈
地址:https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/ <?php /** 定义栈的数据结构,请在该类型中实现一 ...
- Selenium KPI接口 屏幕截图
屏幕截图功能常用的有两种: savescreenshot()及 getscreenshotasfile(). 使用格式 self.driver.save_screenshot('baidu.png') ...
- Jmeter tcp 返回500,但服务器收到请求
解决方法:再end of line(Eol)bytes value 正确写上报文最后两位十进制字节码
- cypress 在 typescript 项目中报错找不到 'tslib'
原文链接:https://blog.jijian.link/2020-08-11/cypress-typescript-cannot-find-module-tslib/ cypress 在 type ...
- css 技巧:利用 after 伪对象和 background 属性实现 img 图片标签占位图
同步发布:https://blog.jijian.link/2020-04-15/css-img-after-placeholder/ 如图: 图片加载失败了,在浏览器会默认显示一张破图.受各种网速. ...
- .NET周刊【3月第1期 2025-03-02】
国内文章 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章 https://www.cnblogs.com/shanyou/p/18737657 2025年2月25日,.NET ...
- Golang Linux、Windows、Mac 下交叉编译
前言 Golang 支持交叉编译, 即同一份代码,在一个平台上生成,然后可以在另外一个平台去执行. 之前写过一篇 Golang windows下 交叉编译 感觉写的不够全面,这篇作为补充. 交叉编译 ...