当前位置:网站首页>剑指 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;
}
};
边栏推荐
- 常见的最优化方法
- Codeforces Round #715 (Div. 2) D. Binary Literature
- Solution to the palindrome string (Luogu p5041 haoi2009)
- AtCoder Grand Contest 013 E - Placing Squares
- The connection and solution between the shortest Hamilton path and the traveling salesman problem
- Full Permutation Code (recursive writing)
- 2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
- Simply sort out the types of sockets
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- sync. Interpretation of mutex source code
猜你喜欢
Implement a fixed capacity stack
浅谈JVM(面试常考)
Sword finger offer 58 - ii Rotate string left
[jailhouse article] look mum, no VM exits
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
[practical skills] technical management of managers with non-technical background
Full Permutation Code (recursive writing)
Little known skills of Task Manager
Gbase database helps the development of digital finance in the Bay Area
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
随机推荐
Transform optimization problems into decision-making problems
Sword finger offer 04 Search in two-dimensional array
Simply sort out the types of sockets
剑指 Offer 53 - II. 0~n-1中缺失的数字
剑指 Offer 09. 用两个栈实现队列
Kubedm series-00-overview
Software test -- 0 sequence
个人开发的渗透测试工具Satania v1.2更新
每日一题-无重复字符的最长子串
Pointnet++ learning
One question per day 1765 The highest point in the map
Little known skills of Task Manager
Detailed explanation of expression (csp-j 2021 expr) topic
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
使用Electron开发桌面应用
1996. number of weak characters in the game
SAP method of modifying system table data
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Sword finger offer 06 Print linked list from beginning to end
[cloud native] record of feign custom configuration of microservices