当前位置:网站首页>Force deduction solution summary 729- my schedule I
Force deduction solution summary 729- my schedule I
2022-07-05 17:26:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Achieve one MyCalendar Class to store your schedule . If the schedule to be added does not cause Repeat Booking , You can store this new schedule .
When there is some time overlap between the two schedules ( For example, both schedules are in the same time ), It will produce Repeat Booking .
The schedule can use a pair of integers start and end Express , The time here is a half open interval , namely [start, end), The set of real Numbers x For the range of , start <= x < end .
Realization MyCalendar class :
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 .
Example :
Input :
["MyCalendar", "book", "book", "book"]
[[], [10, 20], [15, 25], [20, 30]]
Output :
[null, true, false, true]
explain :
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False , This schedule cannot be added to the calendar , Because of time 15 Has been booked by another schedule .
myCalendar.book(20, 30); // return True , This schedule can be added to the calendar , Because the first schedule is booked at less than each time 20 , And does not include time 20 .
Tips :
0 <= start < end <= 109
Each test case , call book The maximum number of methods is 1000 Time .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/my-calendar-i
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * With two List Loading starts and ends ,list1 Date of storage start ,list2 Date of storage end . * Each time a new date start When , All queries are in list1 and start An equal or smaller number s1, Then find the corresponding end date e1. * There are four situations * start>=e1: Then put start and end Insert into List in ; * start<e1: disqualification , return false
Code :
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;
}
/**
* Two points search , Returns a value equal to or less than
*
* @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;
}
}
}边栏推荐
- URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
- Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
- 【性能测试】jmeter+Grafana+influxdb部署实战
- Browser rendering principle and rearrangement and redrawing
- 忽米沄析:工业互联网标识解析与企业信息系统的融合应用
- stirring! 2022 open atom global open source summit registration is hot!
- 【beanshell】数据写入本地多种方法
- 漫画:一道数学题引发的血案
- 漫画:有趣的海盗问题 (完整版)
- 干货!半监督预训练对话模型 SPACE
猜你喜欢
基于Redis实现延时队列的优化方案小结
![[Web attack and Defense] WAF detection technology map](/img/7c/60a25764950668ae454b2bc08fe57e.png)
[Web attack and Defense] WAF detection technology map

哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?

Using C language to realize palindrome number

CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计

Embedded-c Language-1

Practical example of propeller easydl: automatic scratch recognition of industrial parts

Rider set the highlighted side of the selected word, remove the warning and suggest highlighting

CMake教程Step1(基本起点)
一文了解MySQL事务隔离级别
随机推荐
Thoughtworks 全球CTO:按需求构建架构,过度工程只会“劳民伤财”
力扣解法汇总1200-最小绝对差
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
WebApp开发-Google官方教程
dried food! Semi supervised pre training dialogue model space
Understand the usage of functions and methods in go language
7.Scala类
Detailed explanation of printf() and scanf() functions of C language
Embedded UC (UNIX System Advanced Programming) -2
Complete solution instance of Oracle shrink table space
Design of electronic clock based on 51 single chip microcomputer
Three traversal methods of binary tree
CMake教程Step5(添加系统自检)
EasyX second lesson
Tips for extracting JSON fields from MySQL
云安全日报220705:红帽PHP解释器发现执行任意代码漏洞,需要尽快升级
mysql中取出json字段的小技巧
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition