当前位置:网站首页>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;
}
};
边栏推荐
- Introduction et expérience de wazuh open source host Security Solution
- [practical skills] how to do a good job in technical training?
- 2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
- [practical skills] technical management of managers with non-technical background
- Sqlmap tutorial (II) practical skills I
- Appium automation test foundation - Summary of appium test environment construction
- Collection: programming related websites and books
- js快速将json数据转换为url参数
- Binary search template
- “磐云杯”中职网络安全技能大赛A模块新题
猜你喜欢

Wazuh开源主机安全解决方案的简介与使用体验
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

6. Logistic model

Introduction and experience of wazuh open source host security solution

Error ora-28547 or ora-03135 when Navicat connects to Oracle Database

Wazuh開源主機安全解决方案的簡介與使用體驗

Smart construction site "hydropower energy consumption online monitoring system"

智慧工地“水电能耗在线监测系统”

leetcode-6110:网格图中递增路径的数目

SQLMAP使用教程(二)实战技巧一
随机推荐
可变电阻器概述——结构、工作和不同应用
1041 Be Unique
Daily question 1688 Number of matches in the competition
1040 Longest Symmetric String
EOJ 2021.10 E. XOR tree
SQLMAP使用教程(一)
RGB LED infinite mirror controlled by Arduino
shared_ Repeated release heap object of PTR hidden danger
How to adjust bugs in general projects ----- take you through the whole process by hand
【云原生】微服务之Feign自定义配置的记录
[rust notes] 17 concurrent (Part 1)
Real time clock (RTC)
The connection and solution between the shortest Hamilton path and the traveling salesman problem
Overview of variable resistors - structure, operation and different applications
1039 Course List for Student
SPI 详解
js快速将json数据转换为url参数
Doing SQL performance optimization is really eye-catching
SPI details
F - Two Exam(AtCoder Beginner Contest 238)