当前位置:网站首页>Li Kou 729. My schedule I
Li Kou 729. My schedule I
2022-07-23 13:08:00 【Three watch ghost】
Title source :https://leetcode.cn/problems/my-calendar-i/
General meaning :
Set up a MyCalendar class , It includes the following functions :
- MyCalendar() Initialize calendar object .
- boolean book(int start, int end) If the schedule can be successfully added to the calendar without causing duplicate bookings , return true . otherwise , return false And don't add the schedule to the calendar .
Ideas
Use the segment tree to save the schedule , Every time you add a schedule , Mark the interval corresponding to the schedule in the segment tree . Before each addition , First check whether there is any added time period in the current schedule interval , If yes, do not add this schedule .
Because the number of nodes used in the segment tree is about the range of nodes 4 times , The range of ontology nodes is [0, 109], Too much scope , It is not suitable for directly applying for space . Therefore, the dynamic opening method is adopted , Every time you put in a schedule , Dynamically create segment tree nodes related to the schedule interval . And use lazy tags to mark whether the intervals covered by the current node have been scheduled .
The specific operations of adding a schedule are as follows :
- First, judge whether the corresponding interval of the schedule has been added , When querying, you can judge whether nodes have been created in the query interval by lazy tags : If there are lazy tags or nodes in the query interval are created , Then the interval must have been added
- If the whole interval has not been inserted , Then the dynamic opening point is inserted into the schedule
Code :
public class MyCalendar_SegmentTree {
Set<Integer> tree; // Indicates that there is a predetermined interval within the node range
Set<Integer> lazy; // It means that all nodes are scheduled
int BORDER; // Maximum range of nodes
public MyCalendar_SegmentTree() {
tree = new HashSet<>();
lazy = new HashSet<>();
BORDER = (int) 1e9;
}
public boolean book(int start, int end) {
// First, query whether the current schedule interval has been inserted
if (query(start, end - 1, 0, BORDER, 1)) {
return false;
}
// Insert current schedule
update(start, end - 1, 0, BORDER, 1);
return true;
}
/** * Inquire about [start, end] Whether there are segment tree nodes in the interval , If yes, it means that there is a predetermined part in the interval * * @param start * @param end * @param l The left boundary of the current line segment tree node * @param r The right boundary of the current line segment tree node * @param idx Current segment tree node number * @return */
public boolean query(int start, int end, int l, int r, int idx) {
// The current segment tree node does not contain the query range
if (r < start || end < l) {
return false;
}
// If the range of nodes is predetermined , Go straight back to true
if (lazy.contains(idx)) {
return true;
}
// The range of current segment tree nodes is within the query range
if (start <= l && r <= end) {
// So as long as the segment tree node exists , It means that the node range of the line segment tree has been predetermined
return tree.contains(idx);
}
int mid = (l + r) >> 1;
// The query range is only in the left node of the segment tree
if (end <= mid) {
return query(start, end, l, mid, idx * 2);
} else if (mid < start) {
// The query range is only in the right node of the segment tree
return query(start, end, mid + 1, r, idx * 2 + 1);
} else {
// The query range spans two sub nodes of the segment tree
return query(start, end, l, mid, idx * 2) || query(start, end, mid + 1, r, idx * 2 + 1);
}
}
/** * Scheduled interval [start, end] * * @param start * @param end * @param l The left boundary of the current line segment tree node * @param r The right boundary of the current line segment tree node * @param idx Current segment tree node number */
public void update(int start, int end, int l, int r, int idx) {
if (r < start || end < l) {
return;
}
// The range of the current segment tree node is within the update range
if (start <= l && r <= end) {
tree.add(idx);
lazy.add(idx);
} else {
int mid = (l + r) >> 1;
if (mid >= end) {
// The update range is only at the left node of the segment tree
update(start, end, l, mid, idx * 2);
} else if (mid < start) {
// The update range is only at the right node of the segment tree
update(start, end, mid + 1, r, idx * 2 + 1);
} else {
// The update range spans the left and right nodes of the segment tree
update(start, end, l, mid, idx * 2);
update(start, end, mid + 1, r, idx * 2 + 1);
}
// There is a predetermined interval in the node range of the current line segment tree
tree.add(idx);
}
}
public static void main(String[] args) {
MyCalendar_SegmentTree tree = new MyCalendar_SegmentTree();
System.out.println(tree.book(10, 20));
System.out.println(tree.book(15, 25));
System.out.println(tree.book(20, 30));
}
}
边栏推荐
猜你喜欢

zabbix监控详细安装到部署

信號完整性(SI)電源完整性(PI)學習筆記(三十二)電源分配網路(四)

HCIA----07 ACL-Net

SAR成像之点目标仿真(三)—— 仿真结果分析

Simple use of psutil monitoring

Static route configuration instance learning record

STP configuration instance learning record

4D天线阵列布局设计

Secret key remote login server to realize secret free login

北汇信息12岁啦|Happy Birthday
随机推荐
信号完整性(SI)电源完整性(PI)学习笔记(三十一)电源分配网路(三)
Redis如何实现持久化?详细讲解RDB的三种触发机制及其优缺点,带你快速掌握RDB
nfs服务部署笔记
静态路由配置实例学习记录
SAR成像之点目标仿真(一)—— 数学模型
SAR成像之点目标仿真(三)—— 仿真结果分析
C#随机生成一个分数,判断其成绩等级(优、良、中、差、不及格)
Summary of basic SQL operations
PPP configuration instance learning record
Step on the electric render process renderer to solve the problem of require is not defined
numpy:矩阵的元素选取
Static routing principle and configuration
OSPF 多区域配置实例学习记录
写一个可执行文件依赖.so的测试用例
STP configuration instance learning record
yum安装LNMP服务部署
Intégrité du signal (si) intégrité de l'alimentation électrique (PI) notes d'apprentissage (32) Réseau de distribution d'énergie (4)
2020-10-16
Telnet configuration instance learning record
0 array leetcode605. Flower planting problem