当前位置:网站首页>Sword finger offer II 058: schedule
Sword finger offer II 058: schedule
2022-07-05 06:09:00 【Chrysanthemum headed bat】
The finger of the sword Offer II 058: calendar
subject
Topic linking
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
Problem solving
Method 1 :map+ Two points search
because map Will automatically key Sort , So sort the left boundary directly .
Find... By bisection , Find greater than or equal to end, The left border .
Just make , The right boundary ratio of the previous one start Small can be inserted successfully
class MyCalendar {
public:
map<int,int> mp;
MyCalendar() {
}
bool book(int start, int end) {
map<int,int>::iterator it=mp.lower_bound(end);// return key value Greater than or equal to end The first position
if(it==mp.begin()||(--it)->second<=start){
mp[start]=end;
return true;
}
return false;
}
};
边栏推荐
- leetcode-6108:解密消息
- Introduction to convolutional neural network
- 数据可视化图表总结(一)
- [article de jailhouse] jailhouse hypervisor
- Simple knapsack, queue and stack with deque
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
- Navicat连接Oracle数据库报错ORA-28547或ORA-03135
- [rust notes] 16 input and output (Part 2)
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- AtCoder Grand Contest 013 E - Placing Squares
猜你喜欢
Smart construction site "hydropower energy consumption online monitoring system"
Liunx starts redis
CF1634 F. Fibonacci Additions
【Jailhouse 文章】Jailhouse Hypervisor
Dichotomy, discretization, etc
Appium automation test foundation - Summary of appium test environment construction
Introduction et expérience de wazuh open source host Security Solution
AtCoder Grand Contest 013 E - Placing Squares
SQLMAP使用教程(二)实战技巧一
智慧工地“水电能耗在线监测系统”
随机推荐
Daily question 1342 Number of operations to change the number to 0
Doing SQL performance optimization is really eye-catching
Appium基础 — 使用Appium的第一个Demo
leetcode-6110:网格图中递增路径的数目
1040 Longest Symmetric String
shared_ Repeated release heap object of PTR hidden danger
【实战技能】非技术背景经理的技术管理
Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
Wazuh開源主機安全解决方案的簡介與使用體驗
Individual game 12
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
【Rust 笔记】15-字符串与文本(上)
Open source storage is so popular, why do we insist on self-development?
【Rust 笔记】17-并发(下)
7. Processing the input of multidimensional features
Full Permutation Code (recursive writing)
927. 三等分 模拟
Introduction et expérience de wazuh open source host Security Solution
[rust notes] 15 string and text (Part 1)