当前位置:网站首页>【729. 我的日程安排表 I】
【729. 我的日程安排表 I】
2022-07-05 16:13:00 【千北@】
来源:力扣(LeetCode)
描述:
实现一个 MyCalendar
类来存放你的日程安排。如果要添加的日程安排不会造成 重复预订 ,则可以存储这个新的日程安排。
当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生 重复预订 。
日程可以用一对整数 start
和 end
表示,这里的时间是半开区间,即 [start, end)
, 实数 x
的范围为, start <= x < end
。
实现 MyCalendar
类:
- MyCalendar() 初始化日历对象。
- boolean book(int start, int end) 如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 true 。否则,返回 false 并且不要将该日程安排添加到日历中。
示例:
输入:
["MyCalendar", "book", "book", "book"]
[[], [10, 20], [15, 25], [20, 30]]
输出:
[null, true, false, true]
解释:
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False ,这个日程安排不能添加到日历中,因为时间 15 已经被另一个日程安排预订了。
myCalendar.book(20, 30); // return True ,这个日程安排可以添加到日历中,因为第一个日程安排预订的每个时间都小于 20 ,且不包含时间 20 。
提示:
- 0 <= start < end <= 109
- 每个测试用例,调用 book 方法的次数最多不超过 1000 次。
方法一:直接遍历
我们记录下所有已经预订的课程安排区间,当我们预订新的区间 [start,end)
时,此时检查当前已经预订的每个日程安排是否与新日程安排冲突。若不冲突,则可以添加新的日程安排。
- 对于两个区间 [s1, e1)和 [s2, e2),如果二者没有交集,则此时应当满足 s1 ≥ e2 或者 s2 ≥ e1,这就意味着如果满足 s1 < e2 并且 s2 < e1 ,则两者会产生交集。
代码:
class MyCalendar {
private:
vector<pair<int, int>> booked;
public:
MyCalendar() {
}
public:
bool book(int start, int end) {
for (auto &[l, r] : booked) {
if (l < end && start < r) {
return false;
}
}
booked.emplace_back(start, end);
return true;
}
};
执行用时:80 ms, 在所有 C++ 提交中击败了77.49%的用户
内存消耗:36.6 MB, 在所有 C++ 提交中击败了97.08%的用户
复杂度分析
时间复杂度:O(n2), 其中 n 表示日程安排的数量。由于每次在进行预订时,都需要遍历所有已经预订的行程安排。
空间复杂度: O(n),其中 n 表示日程安排的数量。需要保存所有已经预订的行程。
方法二:二分查找
如果我们按时间顺序维护日程安排,则可以通过二分查找日程安排的情况来检查新日程安排是否可以预订,若可以预订则在排序结构中更新插入日程安排。
- 需要一个数据结构能够保持元素排序和支持快速插入,可以用
TreeSet
来构建。对于给定的区间[start,end)
,我们每次查找起点大于等于end
的第一个区间 [ l1, r1 ),同时紧挨着 [ l1, r1) 的前一个区间为 [ l2, r2),此时如果满足 r2 ≤ start < end ≤ l1 ,则该区间可以预订。
代码:
class MyCalendar {
private:
set<pair<int, int>> booked;
public:
MyCalendar() {
}
public:
bool book(int start, int end) {
auto it = booked.lower_bound({
end, 0});
if (it == booked.begin() || (--it)->second <= start) {
booked.emplace(start, end);
return true;
}
return false;
}
};
执行用时:64 ms, 在所有 C++ 提交中击败了98.77%的用户
内存消耗:37.9 MB, 在所有 C++ 提交中击败了53.51%的用户
复杂度分析
时间复杂度: O(nlogn), 其中 n 表示日程安排的数量。由于每次在进行预订时,都需要进行二分查找,需要的时间为 O(logn)。
空间复杂度: O(n),其中 n 表示日程安排的数量。需要保存所有已经预订的行程。
方法三:线段树
代码:
class MyCalendar {
unordered_set<int> tree, lazy;
public:
bool query(int start, int end, int l, int r, int idx) {
if (r < start || end < l) {
return false;
}
/* 如果该区间已被预订,则直接返回 */
if (lazy.count(idx)) {
return true;
}
if (start <= l && r <= end) {
return tree.count(idx);
}
int mid = (l + r) >> 1;
return query(start, end, l, mid, 2 * idx) ||
query(start, end, mid + 1, r, 2 * idx + 1);
}
void update(int start, int end, int l, int r, int idx) {
if (r < start || end < l) {
return;
}
if (start <= l && r <= end) {
tree.emplace(idx);
lazy.emplace(idx);
} else {
int mid = (l + r) >> 1;
update(start, end, l, mid, 2 * idx);
update(start, end, mid + 1, r, 2 * idx + 1);
tree.emplace(idx);
if (lazy.count(2 * idx) && lazy.count(2 * idx + 1)) {
lazy.emplace(idx);
}
}
}
bool book(int start, int end) {
if (query(start, end - 1, 0, 1e9, 1)) {
return false;
}
update(start, end - 1, 0, 1e9, 1);
return true;
}
};
执行用时:476 ms, 在所有 C++ 提交中击败了5.03%的用户
内存消耗:171.4 MB, 在所有 C++ 提交中击败了7.53%的用户
边栏推荐
- 迁移/home分区
- Games101 notes (I)
- Bs-xx-042 implementation of personnel management system based on SSM
- If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
- 一些认知的思考
- PSPNet | 语义分割及场景分析
- Jarvis OJ shell traffic analysis
- Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
- 解决CMakeList find_package找不到Qt5,找不到ECM
- 不敢买的思考
猜你喜欢
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Seaborn绘制11个柱状图
Cs231n notes (bottom) - applicable to 0 Foundation
Jarvis OJ Flag
文件操作--I/O
Win11如何给应用换图标?Win11给应用换图标的方法
为季前卡牌游戏 MotoGP Ignition Champions 做好准备!
Migrate /home partition
深潜Kotlin协程(二十一):Flow 生命周期函数
Hiengine: comparable to the local cloud native memory database engine
随机推荐
【刷題篇】鹅廠文化衫問題
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Quelques réflexions cognitives
Reduce the cost by 40%! Container practice of redis multi tenant cluster
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
Oneforall installation and use
[brush questions] effective Sudoku
[brush title] goose factory shirt problem
如何将mysql卸载干净
EDI许可证和ICP经营性证有什么区别
普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
【刷题篇】有效的数独
Get ready for the pre-season card game MotoGP ignition champions!
How does win11 change icons for applications? Win11 method of changing icons for applications
不敢买的思考
养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
數據訪問 - EntityFramework集成
Jarvis OJ shell流量分析
树莓派4b安装Pytorch1.11
2020-2022 two-year anniversary of creation