LeetCode 737.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 1MyCalendarThree.book(50, 60); // returns 1MyCalendarThree.book(10, 40); // returns 2MyCalendarThree.book(5, 15); // returns 3MyCalendarThree.book(5, 10); // returns 3MyCalendarThree.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.
- 思路分析
每次向时间轴里插入一个有向线段,返回此时最多的重合次数 - 代码
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;}
}
LeetCode 737.My Calendar III相关推荐
- LeetCode#732 My Calendar My Calendar III
LeetCode#732 My Calendar My Calendar III 题目 Implement a MyCalendarThree class to store your events. ...
- 用BST解决729. My Calendar I 731. My Calendar II 732. My Calendar III
My Calendar的book方法实现指定开始时间.结束时间,在重叠次数要求不同的情况下怎么实现. 729 My Calendar I 要求任意两个事件之间不能重叠.如果要插入的事件和已经插入的事件 ...
- 【?异或】LeetCode 260. Single Number III
LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...
- LeetCode 1306. 跳跃游戏 III
LeetCode 1306. 跳跃游戏 III 题目说明 方法一:带辅助数组的dfs 方法二:不带辅助数组的dfs 题目说明 这里有一个非负整数数组 arr,你最开始位于该数组的起始下标 start ...
- [Swift]LeetCode732. 我的日程安排表 III | My Calendar III
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Day25 LeetCode 216. 组合总和 III 17. 电话号码的字母组合
题目:216. 组合总和 III - 力扣(LeetCode) 思路: 1.递归函数参数和返回值:首先需要两个全局变量一维path数组和二维result数组,path数组用来收集路径上的元素,resu ...
- leetcode 1306. Jump Game III | 1306. 跳跃游戏 III(BFS)
题目 https://leetcode.com/problems/jump-game-iii/ 题解 正如 hint 所说: 用 BFS,跳过的就不跳了,直到全部跳过,即 count == arr.l ...
- leetcode 437. Path Sum III | 437. 路径总和 III(DFS,前缀和)
题目 https://leetcode.com/problems/path-sum-iii/ 题解 DFS+前缀和问题,注意节点可能为 0 的情况(也是因此发现应该用 HashMap 记录前缀和的个数 ...
- leetcode 337. House Robber III | 337. 打家劫舍 III(树形dp;什么情况下dp需要强制包含当前元素?)
题目 https://leetcode.com/problems/house-robber-iii/ 思考:什么情况下 dp 需要强制包含当前元素? dp 过程中,需要包含当前元素 的例子: leet ...
最新文章
- 干掉Navicat:正版,MySQL官方客户端真香!
- Mockplus设计大赛获奖选手专访 | High音:轻松生活,随心嗨音
- ip打包相对路径 vivado_Vivado自定义IP封装流程
- MPI编程的常用接口速查
- 12-函数的定义+自调+实参及形参理解
- 麦块不显示服务器,为什么开不了服务器啊!
- Linux编程考前测试题
- prestashop后台如何删除订单
- Open3d之计算点云凸包
- 兆芯笔试题(2015)找反复数最多的字母的数量以及位置
- Chrome 扩展程序 CrxMouse 优化版 v3.0.1
- Failed to introspect Class [com.ssm.controller.OrderController] from ClassLoader [ParallelWebappClas
- python爬虫怎么下载图片到手机_python爬虫获取京东手机图片的图文教程
- Python变量与字符串
- 关于 window.open() referer 非法请求的问题
- 虾皮系统老出现服务器错误,Shopee卖家须知:虾皮系统什么状况会自动取消订单?...
- Python脚本破解Linux口令(crypt模块)
- VC++开发垃圾文件清理软件之三:程序的界面设计与实现----对话框界面
- 计算机键盘突然不能用了,如果联想笔记本电脑键盘突然无法使用怎么办?
- 部分RFID安全技术