当前位置:网站首页>力扣解法汇总729-我的日程安排表 I
力扣解法汇总729-我的日程安排表 I
2022-07-05 16:55:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
实现一个 MyCalendar 类来存放你的日程安排。如果要添加的日程安排不会造成 重复预订 ,则可以存储这个新的日程安排。
当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生 重复预订 。
日程可以用一对整数 start 和 end 表示,这里的时间是半开区间,即 [start, end), 实数 x 的范围为, start <= x < end 。
实现 MyCalendar 类:
MyCalendar() 初始化日历对象。
boolean book(int start, int end) 如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 true 。否则,返回 false 并且不要将该日程安排添加到日历中。
示例:
输入:
["MyCalendar", "book", "book", "book"]
[[], [10, 20], [15, 25], [20, 30]]
输出:
[null, true, false, true]
解释:
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False ,这个日程安排不能添加到日历中,因为时间 15 已经被另一个日程安排预订了。
myCalendar.book(20, 30); // return True ,这个日程安排可以添加到日历中,因为第一个日程安排预订的每个时间都小于 20 ,且不包含时间 20 。
提示:
0 <= start < end <= 109
每个测试用例,调用 book 方法的次数最多不超过 1000 次。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/my-calendar-i
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 用两个List装载开始和结束,list1存储开始的日期,list2存储结束的日期。 * 每次一个新的日期start的时候,都查询在list1和start相等或者更小的数字s1,然后找其对应的结束日期e1。 * 这里一共有四种情况 * start>=e1:则把start和end插入到List中; * start<e1:不符合条件,返回false
代码:
public class Solution729 {
public static class MyCalendar {
List<Integer> startList = new ArrayList<>();
List<Integer> endList = new ArrayList<>();
public MyCalendar() {
}
public boolean book(int start, int end) {
if (startList.size() == 0) {
startList.add(start);
endList.add(end);
return true;
}
int i = middel2Search(startList, start);
if (i == -1) {
if (end > startList.get(0)) {
return false;
}
startList.add(0, start);
endList.add(0, end);
return true;
}
Integer e1 = endList.get(i);
Integer s2 = Integer.MAX_VALUE;
if (i < startList.size() - 1) {
s2 = startList.get(i + 1);
}
if (start >= e1 && end <= s2) {
startList.add(i + 1, start);
endList.add(i + 1, end);
return true;
}
return false;
}
/**
* 二分查找,返回等于小于其的
*
* @param node
*/
public int middel2Search(List<Integer> list, int node) {
if (list.size() == 0) {
return 0;
}
int start = 0;
int end = list.size() - 1;
while (start <= end) {
int startNode = list.get(start);
int endNode = list.get(end);
if (node < startNode) {
return start - 1;
}
start++;
if (node >= endNode) {
return end;
}
end--;
}
return start - 1;
}
}
}边栏推荐
- WebApp开发-Google官方教程
- Embedded-c Language-2
- winedt常用快捷键 修改快捷键latex编译按钮
- 33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
- 启牛商学院股票开户安全吗?靠谱吗?
- 激动人心!2022开放原子全球开源峰会报名火热开启!
- Deeply cultivate 5g, and smart core continues to promote 5g applications
- Embedded-c language-6
- [Jianzhi offer] 62 The last remaining number in the circle
- 7. Scala class
猜你喜欢
基于Redis实现延时队列的优化方案小结

The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
一文了解MySQL事务隔离级别

WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
![[Jianzhi offer] 63 Maximum profit of stock](/img/b6/c1dec97a23ac13aa53d1d202b83ef5.png)
[Jianzhi offer] 63 Maximum profit of stock

Winedt common shortcut key modify shortcut key latex compile button

兰空图床苹果快捷指令

NPM installation
What are the precautions for MySQL group by

Embedded UC (UNIX System Advanced Programming) -3
随机推荐
MYSQL group by 有哪些注意事项
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
C language to get program running time
Embedded -arm (bare board development) -2
The second day of learning C language for Asian people
关于mysql中的json解析函数JSON_EXTRACT
一口气读懂 IT发展史
Tips for extracting JSON fields from MySQL
Machine learning 02: model evaluation
Database design in multi tenant mode
ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
How can C TCP set heartbeat packets to be elegant?
Practical example of propeller easydl: automatic scratch recognition of industrial parts
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
Wechat official account web page authorization login is so simple
Understand the usage of functions and methods in go language
winedt常用快捷键 修改快捷键latex编译按钮