当前位置:网站首页>An interesting example to illustrate the difference of emplace_ back() and push_ back()
An interesting example to illustrate the difference of emplace_ back() and push_ back()
2022-07-21 02:49:00 【SUNNY_ CHANGQI】
The description of the problem
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.
A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.
Implement the MyCalendarTwo class:
MyCalendarTwo() Initializes the calendar object.
boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.
source : Power button (LeetCode)
link :https://leetcode.cn/problems/my-calendar-ii
an example
Input
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output
[null, true, true, true, false, true, true]
Explanation
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
source : Power button (LeetCode)
link :https://leetcode.cn/problems/my-calendar-ii
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The codes 1
class MyCalendarTwo {
private:
vector<pair<int, int>> books;
vector<pair<int, int>> overlaps;
public:
MyCalendarTwo() {
}
bool book(int start, int end) {
for (auto &[l, r] : overlaps) {
if (start < r && l <end) {
return false;
}
}
for (auto &[l, r] : books) {
if (start < r && l <end) {
overlaps.emplace_back(make_pair(max(l, start), min(r, end)));
}
}
books.emplace_back(make_pair(start, end));
return true;
}
};
/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo* obj = new MyCalendarTwo(); * bool param_1 = obj->book(start,end); */
The codes 2
class MyCalendarTwo {
private:
vector<pair<int, int>> books;
vector<pair<int, int>> overlaps;
public:
MyCalendarTwo() {
}
bool book(int start, int end) {
for (auto &[l, r] : overlaps) {
if (start < r && l <end) {
return false;
}
}
for (auto &[l, r] : books) {
if (start < r && l <end) {
overlaps.push_back(make_pair(max(l, start), min(r, end)));
}
}
books.push_back(make_pair(start, end));
return true;
}
};
/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo* obj = new MyCalendarTwo(); * bool param_1 = obj->book(start,end); */
The codes 3
class MyCalendarTwo {
private:
vector<pair<int, int>> books;
vector<pair<int, int>> overlaps;
public:
MyCalendarTwo() {
}
bool book(int start, int end) {
for (auto &[l, r] : overlaps) {
if (start < r && l <end) {
return false;
}
}
for (auto &[l, r] : books) {
if (start < r && l <end) {
overlaps.emplace_back(max(l, start), min(r, end));
}
}
books.emplace_back(start, end);
return true;
}
};
/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo* obj = new MyCalendarTwo(); * bool param_1 = obj->book(start,end); */
边栏推荐
- 记录uni-app 打包成apk后获取定位
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- Award winning research | what does the perfect ar development platform look like to make virtual reality?
- How do independent website sellers use Facebook homepage for social media marketing?
- Qinghai VR fire simulation drill system meets the training needs of many people and scenes
- C语言中getchar()函数的详解
- 标志信号(flag)
- HMS Core音频编辑服务支持7种音频特效,助力一站式音频处理
- To get to the bottom: Principle Analysis of Objective-C correlation attribute
- 密码密钥硬编码检查
猜你喜欢
MongoDB数据库简介、安装和基本使用
excel数据条怎么设置百分比颜色?excel数据条按百分比自动填充颜色教程
【ArcGIS微课1000例】0029:ArcGIS绘制平行线(构造平行公路)
vivo官网APP全机型UI适配方案
Detailed explanation of getchar () function in C language
Kingdee's "answer sheet" forecast in the first half of the year: key customer strategy continued to break through, and arr increased by more than 45% year-on-year
元宇宙 3D 开荒场
Ilrunitme foreach has GC
Real time debugging practice based on attach to process
How to convert Excel to word format? Method of converting Excel to word format
随机推荐
[Muduo log system 1] logger output
泛型和包装类
gerrit系统如何配置访问控制
虚实相生,构建数智生活|HMS Core. Sparkle应用创新分论坛报名启动
Jenkins Automated Deployment
Jenkins自动化部署
Common environment configuration nouns in development -dev, sit, pro, FAC, etc
[Android Development iOS Series] Language: SWIFT vs kotlin
NFT潮鞋AR互动零基础教程来啦!
Ilrunitme foreach has GC
中文主播也能海外带货!同声传译助直播类应用开拓海外市场
【MUDUO SOCKET】InetAddress 封装SOCKET地址类型
A detailed explanation of the implementation principle of go Distributed Link Tracking
How does HMS core security detection service help freshmen prevent Telecom fraud?
HMS core graphics and image technology shows the latest functions and application scenarios, and accelerates the construction of digital intelligence life
HMS Core图形图像技术展现最新功能和应用场景,加速构建数智生活
Returns the quarter and year of the date provided
How to convert Excel to word format? Method of converting Excel to word format
爱学啊的博客-人生苦短,只做好课!
C语言中getchar()函数的详解