当前位置:网站首页>LeetCode 731. My schedule II
LeetCode 731. My schedule II
2022-07-06 06:23:00 【Sasakihaise_】

【 Interval splitting and merging 】 I think it's complicated , Split the double overlapping interval , If you want to split an interval, you should consider the same endpoint .
class MyCalendarTwo {
// Splitting and merging of double ordered sets 1:27 2:47
TreeMap<Integer, Integer> m1 = new TreeMap();
TreeMap<Integer, Integer> m2 = new TreeMap();
public MyCalendarTwo() {
}
public boolean book(int start, int end) {
Integer k2 = m2.lowerKey(end);
if (k2 != null) {
if (m2.get(k2) > start) {
return false;
}
}
Integer key = m1.lowerKey(end);
if (key == null) {
m1.put(start, end);
return true;
}
int value = m1.get(key);
if (value <= start) {
m1.put(start, end);
return true;
}
while (key != null) {
if (m1.get(key) > start) {
value = m1.get(key);
// key start value end Overlap is :max(key, start) min(value, end)
// Left is min(key, start) max(key, start) On the right is min(value, end) max(value, end)
int l1 = Math.min(key, start);
int e1 = Math.max(key, start);
int l2 = Math.min(value, end);
int e2 = Math.max(value, end);
m2.put(e1, l2);
m1.remove(key);
if (l2 != e2) {
m1.put(l2, e2);
}
if (l1 != e1) {
start = l1;
end = e1;
key = m1.lowerKey(end);
if (key == null) {
m1.put(start, end);
return true;
}
} else {
break;
}
} else {
m1.put(start, end);
break;
}
}
return true;
}
}
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo obj = new MyCalendarTwo();
* boolean param_1 = obj.book(start,end);
*/【 Optimize 】 In fact, it doesn't matter if the primary interval overlaps with the secondary interval , Because first of all, we will go to the double interval judgment .
class MyCalendarTwo {
// double TreeMap 2:55
TreeMap<Integer, Integer> m1 = new TreeMap();
TreeMap<Integer, Integer> m2 = new TreeMap();
public MyCalendarTwo() {
}
public boolean book(int start, int end) {
Integer k2 = m2.lowerKey(end);
if (k2 != null && m2.get(k2) > start) {
return false;
}
Integer key = m1.lowerKey(end);
while (key != null) {
int value = m1.get(key);
if (value <= start) {
break;
}
m1.remove(key);
m2.put(Math.max(start, key), Math.min(end, value));
start = Math.min(start, key);
end = Math.max(end, value);
key = m1.lowerKey(end);
}
m1.put(start, end);
return true;
}
}
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo obj = new MyCalendarTwo();
* boolean param_1 = obj.book(start,end);
*/
边栏推荐
- 黑猫带你学UFS协议第18篇:UFS如何配置逻辑单元(LU Management)
- 浅谈专项测试之弱网络测试
- Simulation volume leetcode [general] 1091 The shortest path in binary matrix
- Summary of anomaly detection methods
- 还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
- 10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
- F - true liars (category and search set +dp)
- [C language] qsort function
- Left matching principle of joint index
- Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
猜你喜欢

SourceInsight Chinese garbled

MFC关于长字符串unsigned char与CString转换及显示问题
![[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development](/img/63/dc729d3f483fd6088cfa7b6fb45ccb.png)
[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development

Delete the variables added to watch1 in keil MDK

oscp raven2靶机渗透过程

[no app push general test plan

Pat (Grade B) 2022 summer exam

【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能

二维码的前世今生 与 六大测试点梳理
![[postman] collections configuration running process](/img/09/bcd9fd6379fa724671ffd09278442e.png)
[postman] collections configuration running process
随机推荐
Postman核心功能解析-参数化和测试报告
二维码的前世今生 与 六大测试点梳理
Customize the gateway filter factory on the specified route
Testing of web interface elements
全链路压测:构建三大模型
模拟卷Leetcode【普通】1091. 二进制矩阵中的最短路径
曼哈顿距离与曼哈顿矩形-打印回字型矩阵
Simulation volume leetcode [general] 1109 Flight reservation statistics
Simulation volume leetcode [general] 1314 Matrix area and
私人云盘部署
Win10 cannot operate (delete, cut) files
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
F - true liars (category and search set +dp)
LeetCode 732. 我的日程安排表 III
模拟卷Leetcode【普通】1061. 按字典序排列最小的等效字符串
模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
Simulation volume leetcode [general] 1061 Arrange the smallest equivalent strings in dictionary order
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
Simulation volume leetcode [general] 1249 Remove invalid parentheses
PHP uses redis to implement distributed locks