当前位置:网站首页>LeetCode 731. 我的日程安排表 II
LeetCode 731. 我的日程安排表 II
2022-07-06 06:02:00 【Sasakihaise_】
【区间拆分与合并】想复杂了,把双次重叠区间给拆分开了,如果进行区间拆分的话要考虑端点相同的情况。
class MyCalendarTwo {
// 双有序集合拆分与合并 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 重叠是:max(key, start) min(value, end)
// 左是 min(key, start) max(key, start) 右是 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);
*/
【优化】其实一重区间和二重区间有重叠也是没关系的,因为首先就会去二重区间判断。
class MyCalendarTwo {
// 双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);
*/
边栏推荐
- 华为路由器忘记密码怎么恢复
- Novice entry SCM must understand those things
- Linux regularly backs up MySQL database
- Auto. JS learning notes 17: basic listening events and UI simple click event operations
- 公司视频加速播放
- 数字三角形模型 AcWing 1015. 摘花生
- 《卓有成效的管理者》读书笔记
- GTSAM中李群的运用
- Pay attention to the details of pytoch code, and it is easy to make mistakes
- Grant Yu, build a web page you want from 0
猜你喜欢
Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Novice entry SCM must understand those things
Practice sharing: how to safely and quickly migrate from CentOS to openeuler
wib3.0 跨越,在跨越(ง •̀_•́)ง
【微信小程序】搭建开发工具环境
Detailed explanation of BF and KMP
J'ai un chaton.
IPv6 comprehensive experiment
Market development prospect and investment risk assessment report of China's humidity sensor industry from 2022 to 2028
随机推荐
华为BFD的配置规范
Idea new UI usage
Configuring OSPF GR features for Huawei devices
H3C V7 switch configuration IRF
Reading notes of effective managers
Software test interview questions - Test Type
P问题、NP问题、NPC问题、NP-hard问题详解
As3013 fire endurance test of cable distribution system
华为路由器如何配置静态路由
VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
【论文代码】SML部分代码阅读
Luogu [Beginner Level 4] array p1427 number game of small fish
Leetcode 701 insertion operation in binary search tree -- recursive method and iterative method
Introduction to promql of # yyds dry goods inventory # Prometheus
关于 PHP 启动 MongoDb 找不到指定模块问题
[C language syntax] the difference between typedef struct and struct
[Thesis code] SML part code reading
Gtest之TEST宏的用法
IPv6 comprehensive experiment
[happy Spring Festival] if you feel happy, dance