当前位置:网站首页>[731. My schedule II]
[731. My schedule II]
2022-07-21 01:04:00 【[email protected]】
source : Power button (LeetCode)
describe :
Achieve one MyCalendar
Class to store your schedule . If the time to be added does not result in triple booking , 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 the three schedules have some time overlap ( For example, all three schedules are at the same time ), There will be triple bookings .
Every time you call MyCalendar.book
When the method is used , If the schedule can be successfully added to the calendar without triple booking , 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 :
MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(50, 60); // returns true
MyCalendar.book(10, 40); // returns true
MyCalendar.book(5, 15); // returns false
MyCalendar.book(5, 10); // returns true
MyCalendar.book(25, 55); // returns true
explain :
The first two schedules can be added to the calendar . The third schedule will lead to double booking , But it can be added to the calendar .
Fourth schedule activity (5,15) Can't add to calendar , Because it leads to triple booking .
The fifth schedule (5,10) Can be added to the calendar , Because it doesn't use the time that has been double reserved 10.
Sixth schedule (25,55) Can be added to the calendar , Because of time [25,40] Double booking with the Third Schedule ;
Time [40,50] Will book separately , Time [50,55) Double booking with the second schedule .
Tips :
Each test case , call MyCalendar.book The function is no more than 1000 Time .
Call function MyCalendar.book(start, end) when , start and end The value range of is [0, 109].
Code :
class MyCalendarTwo {
public:
MyCalendarTwo() {
}
bool book(int start, int end) {
for (auto &[l, r] : overlaps) {
if (l < end && start < r) {
return false;
}
}
for (auto &[l, r] : booked) {
if (l < end && start < r) {
overlaps.emplace_back(max(l, start), min(r, end));
}
}
booked.emplace_back(start, end);
return true;
}
private:
vector<pair<int, int>> booked;
vector<pair<int, int>> overlaps;
};
Execution time :76 ms, In all C++ Defeated in submission 97.36% Users of
Memory consumption :33.1 MB, In all C++ Defeated in submission 96.44% Users of
Complexity analysis
Time complexity :O(n2), among n Indicates the number of schedules . Because every time I make a reservation , You need to traverse all the scheduled schedules .
Spatial complexity : O(n), among n Indicates the number of schedules . You need to save all the scheduled trips .
Code :
class MyCalendarTwo {
public:
MyCalendarTwo() {
}
bool book(int start, int end) {
int ans = 0;
int maxBook = 0;
cnt[start]++;
cnt[end]--;
for (auto &[_, freq] : cnt) {
maxBook += freq;
ans = max(maxBook, ans);
if (maxBook > 2) {
cnt[start]--;
cnt[end]++;
return false;
}
}
return true;
}
private:
map<int, int> cnt;
};
Execution time :192 ms, In all C++ Defeated in submission 79.55% Users of
Memory consumption :38 MB, In all C++ Defeated in submission 55.34% Users of
Complexity analysis
Time complexity :O(n2), among n Number of schedules . Each time the maximum reservation is calculated, all schedules need to be traversed .
Spatial complexity :O(n), among n Number of schedules . Need space to store all schedule counts , The space needed is O(n).
Code :
class MyCalendarTwo {
public:
MyCalendarTwo() {
}
void update(int start, int end, int val, int l, int r, int idx) {
if (r < start || end < l) {
return;
}
if (start <= l && r <= end) {
tree[idx].first += val;
tree[idx].second += val;
} else {
int mid = (l + r) >> 1;
update(start, end, val, l, mid, 2 * idx);
update(start, end, val, mid + 1, r, 2 * idx + 1);
tree[idx].first = tree[idx].second + max(tree[2 * idx].first, tree[2 * idx + 1].first);
}
}
bool book(int start, int end) {
update(start, end - 1, 1, 0, 1e9, 1);
if (tree[1].first > 2) {
update(start, end - 1, -1, 0, 1e9, 1);
return false;
}
return true;
}
private:
unordered_map<int, pair<int, int>> tree;
};
Execution time :912 ms, In all C++ Defeated in submission 5.09% Users of
Memory consumption :293.8 MB, In all C++ Defeated in submission 5.09% Users of
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/202/202207200106520640.html
边栏推荐
- redisconnectionfactory could not autowired
- [translation] dynamic query in C # expression
- 股票问题一网打尽
- 企业在什么情况下有引入分布式数据库的必要性?
- Leetcode 做题语法补充总结笔记
- Ansible introduction and installation
- Video playback
- Voulez - vous assurer la sécurité des logiciels à faible coût? Cinq missions de sécurité méritent d'être examinées
- 构建乘积数组
- Leetcode- number of occurrences of numbers in the array (single dog problem)
猜你喜欢
程序环境和预处理详解
网络流,二分图与图的匹配
Web APIs DOM page special effects scrolling events and loading events
零信任和SASE有什么不一样?答案其实并不重要
电气成套设备制造企业项目管理难点及解决方案
Web APIs DOM event delegation + comprehensive case
开发者必读:2022年移动应用运营增长洞察白皮书
STM32学习---SPI
How to check whether win11 can be upgraded to 22h2? How to upgrade 22h2 in win11
Protocol buffer learning
随机推荐
解锁高评分 | eBay 深耕用户体验,优化大屏幕设备应用
Voulez - vous assurer la sécurité des logiciels à faible coût? Cinq missions de sécurité méritent d'être examinées
DeiT:注意力也能蒸馏
数据仓库开发 SQL 使用技巧总结
建模杂谈系列144 SCLC工程化实验
Net question and answer: is there the most efficient way to check large files in C?
Glue terraform ecology to kubernetes world
org.xml.sax.SAXParseException无法读取方案文档
Win11新Bug任务栏图标不显示的解决方法
NVIDIA NX usage notes
CCTV news "Jinan rent quota invoice by hand" news channel_ People's network
Bubble sort and quick sort
C # understand these 100 + lines of code, and you will really get started (Classic)
Information system project manager core examination center (46) procurement statement of work (SOW)
Win11暂存文件夹是什么?Win11在线升级暂存文件夹在哪
Ansible introduction and installation
【求解AX=b】
[translation] dynamic query in C # expression
How to check whether win11 can be upgraded to 22h2? How to upgrade 22h2 in win11
Web APIs DOM event delegation + comprehensive case