当前位置:网站首页>剑指 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;
}
};
边栏推荐
- Time complexity and space complexity
- 读者写者模型
- 26、 File system API (device sharing between applications; directory and file API)
- 每日一题-无重复字符的最长子串
- Brief introduction to tcp/ip protocol stack
- One question per day 1447 Simplest fraction
- Little known skills of Task Manager
- Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
- [practical skills] technical management of managers with non-technical background
- Implement an iterative stack
猜你喜欢

Some common problems in the assessment of network engineers: WLAN, BGP, switch

剑指 Offer 35.复杂链表的复制

Palindrome (csp-s-2021-palin) solution

Wazuh开源主机安全解决方案的简介与使用体验

游戏商城毕业设计

CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five

SAP method of modifying system table data

Introduction and experience of wazuh open source host security solution
![[jailhouse article] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[jailhouse article] jailhouse hypervisor

Scope of inline symbol
随机推荐
Sword finger offer 53 - ii Missing numbers from 0 to n-1
每日一题-无重复字符的最长子串
kubeadm系列-00-overview
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
Dynamic planning solution ideas and summary (30000 words)
[article de jailhouse] jailhouse hypervisor
Developing desktop applications with electron
1996. number of weak characters in the game
[cloud native] record of feign custom configuration of microservices
Palindrome (csp-s-2021-palin) solution
Zzulioj 1673: b: clever characters???
Collection: programming related websites and books
Over fitting and regularization
2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
Smart construction site "hydropower energy consumption online monitoring system"
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
剑指 Offer 05. 替换空格
One question per day 1765 The highest point in the map
Drawing dynamic 3D circle with pure C language