当前位置:网站首页>Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
2022-07-28 22:20:00 【Calm in the wind and rain】
The finger of the sword Offer II 058. calendar
Please implement a MyCalendar Class to store your schedule . If there is no other schedule for the time to be added , You can store this new schedule .
MyCalendar There is one book(int start, int end) Method . It means in start To end Add a schedule in time , Be careful , The time here is a half open interval , namely [start, end), The set of real Numbers x For the range of , start <= x < end.
When there is some time overlap between the two schedules ( For example, both schedules are in the same time ), There will be repeat bookings .
Every time you call MyCalendar.book When the method is used , If the schedule can be successfully added to the calendar without causing duplicate bookings , return true. otherwise , return false And don't add the schedule to the calendar .
Please follow the steps below to call MyCalendar class : MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
Example :
Input :
[“MyCalendar”,“book”,“book”,“book”]
[[],[10,20],[15,25],[20,30]]
Output : [null,true,false,true]
explain :
MyCalendar myCalendar = new MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false , The second schedule cannot be added to the calendar , Because of time 15 It has been scheduled by the first schedule
MyCalendar.book(20, 30); // returns true , The third schedule can be added to the calendar , Because the first schedule doesn't include time 20
Tips :
Each test case , call MyCalendar.book The function is no more than 1000 Time .
0 <= start < end <= 109
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/fi9suh
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
utilize TreeMap Method to find the largest smaller than the current start Of key And the smallest is greater than the current start Of key Just judge .
Answer key (Java)
class MyCalendar {
private TreeMap<Long, Long> map = new TreeMap<>();
public MyCalendar() {
}
public boolean book(int start, int end) {
Long lower = map.floorKey((long)start);
Long higher = map.ceilingKey((long)start);
if (lower != null && map.get(lower) > start || higher != null && higher < end) {
return false;
}
map.put((long)start, (long)end);
return true;
}
}
/** * Your MyCalendar object will be instantiated and called as such: * MyCalendar obj = new MyCalendar(); * boolean param_1 = obj.book(start,end); */
边栏推荐
- The person in charge of Tencent cloud database borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
- Tensorflow serving high performance machine learning model service system
- [LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
- DHCP and PPPoE protocols and packet capture analysis
- 腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实
- CDN working principle
- mysql create语句能不能用来建立表结构并追加新的记录
- HCIP(11)
- Alibaba cloud CDN practice
- openresty 请求鉴权
猜你喜欢
随机推荐
Summary of the use of hash table set and map when leetcode brushes questions
普源示波器实际的使用效果怎么样
mysql create语句能不能用来建立表结构并追加新的记录
小程序 canvas 生成海报
[binary tree] pseudo palindrome path in binary tree
深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查
Make trouble fishing day by day
Esp8266 Arduino programming example - deep sleep and wake up
Learn kotlin - extension function
Tensorflow serving high performance machine learning model service system
Part 8: creating camera classes
Hcip experiment (15)
罗克韦尔AB PLC RSLogix数字量IO模块基本介绍
array_diff_assoc 元素是数组时不比较数组值的办法
局域网添加DNS服务器进行域名解析
C语言编程规范学习笔记和总结
笔试总结记录
HCIP(9)
The binary search boundary value processing based on leetcode35 is used to clarify the boundary value of the judgment condition using the idea of interval
【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络








