当前位置:网站首页>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;
}
};
边栏推荐
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- 开源存储这么香,为何我们还要坚持自研?
- Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
- liunx启动redis
- Groupbykey() and reducebykey() and combinebykey() in spark
- [jailhouse article] look mum, no VM exits
- Collection: programming related websites and books
- 多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
- One question per day 1447 Simplest fraction
- SQLMAP使用教程(一)
猜你喜欢
![[jailhouse article] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[jailhouse article] jailhouse hypervisor

Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module

Sqlmap tutorial (II) practical skills I

Open source storage is so popular, why do we insist on self-development?

RGB LED infinite mirror controlled by Arduino

How to adjust bugs in general projects ----- take you through the whole process by hand

Typical use cases for knapsacks, queues, and stacks

Introduction and experience of wazuh open source host security solution

R语言【数据集的导入导出】

Dichotomy, discretization, etc
随机推荐
Daily question 1984 Minimum difference in student scores
【Rust 笔记】17-并发(下)
CPU内核和逻辑处理器的区别
F - Two Exam(AtCoder Beginner Contest 238)
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
Liunx starts redis
【Rust 笔记】15-字符串与文本(上)
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Daily question 1688 Number of matches in the competition
How to adjust bugs in general projects ----- take you through the whole process by hand
剑指 Offer II 058:日程表
Introduction and experience of wazuh open source host security solution
R语言【数据集的导入导出】
RGB LED infinite mirror controlled by Arduino
wordpress切换页面,域名变回了IP地址
【Rust 笔记】14-集合(下)
[jailhouse article] look mum, no VM exits
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
Collection: programming related websites and books
leetcode-1200:最小绝对差