当前位置:网站首页>剑指 Offer II 058:日程表
剑指 Offer II 058:日程表
2022-07-05 05:46:00 【菊头蝙蝠】
剑指 Offer II 058:日程表
题目
题目连接
请实现一个 MyCalendar 类来存放你的日程安排。如果要添加的时间内没有其他安排,则可以存储这个新的日程安排。
MyCalendar 有一个 book(int start, int end)方法。它意味着在 start 到 end 时间内增加一个日程安排,注意,这里的时间是半开区间,即 [start, end), 实数 x 的范围为, start <= x < end。
当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生重复预订。
每次调用 MyCalendar.book方法时,如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 true。否则,返回 false 并且不要将该日程安排添加到日历中。
请按照以下步骤调用 MyCalendar 类: MyCalendar cal = new MyCalendar();MyCalendar.book(start, end)
示例:
输入:
["MyCalendar","book","book","book"]
[[],[10,20],[15,25],[20,30]]
输出: [null,true,false,true]
解释:
MyCalendar myCalendar = new MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false ,第二个日程安排不能添加到日历中,因为时间 15 已经被第一个日程安排预定了
MyCalendar.book(20, 30); // returns true ,第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20

解题
方法一:map+二分查找
由于map会自动对 key进行排序,因此直接对左边界排序。
通过二分查找,找到大于等于end,的左边界。
只要使得,上一个的右边界比start小就能插入成功
class MyCalendar {
public:
map<int,int> mp;
MyCalendar() {
}
bool book(int start, int end) {
map<int,int>::iterator it=mp.lower_bound(end);// 返回 key值 大于等于 end 的第一个位置
if(it==mp.begin()||(--it)->second<=start){
mp[start]=end;
return true;
}
return false;
}
};
边栏推荐
- AtCoder Grand Contest 013 E - Placing Squares
- Kubedm series-00-overview
- EOJ 2021.10 E. XOR tree
- Scope of inline symbol
- Drawing dynamic 3D circle with pure C language
- Brief introduction to tcp/ip protocol stack
- Simple knapsack, queue and stack with deque
- 1996. number of weak characters in the game
- “磐云杯”中职网络安全技能大赛A模块新题
- 剑指 Offer 53 - I. 在排序数组中查找数字 I
猜你喜欢

7. Processing the input of multidimensional features

On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech

Sword finger offer 53 - ii Missing numbers from 0 to n-1

AtCoder Grand Contest 013 E - Placing Squares

挂起等待锁 vs 自旋锁(两者的使用场合)

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

6. Logistic model

EOJ 2021.10 E. XOR tree

Reader writer model

智慧工地“水电能耗在线监测系统”
随机推荐
One question per day 1447 Simplest fraction
每日一题-无重复字符的最长子串
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
2020ccpc Qinhuangdao J - Kingdom's power
剑指 Offer 35.复杂链表的复制
Detailed explanation of expression (csp-j 2021 expr) topic
Implement a fixed capacity stack
Sword finger offer 53 - I. find the number I in the sorted array
[jailhouse article] look mum, no VM exits
Over fitting and regularization
Brief introduction to tcp/ip protocol stack
CF1634 F. Fibonacci Additions
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
Control unit
Sword finger offer 04 Search in two-dimensional array
剑指 Offer 05. 替换空格
全排列的代码 (递归写法)
PC寄存器
Personal developed penetration testing tool Satania v1.2 update
Talking about JVM (frequent interview)