当前位置:网站首页>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;
}
}
}
边栏推荐
- NPM installation
- Judge whether a number is a prime number (prime number)
- 这个17岁的黑客天才,破解了第一代iPhone!
- 哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
- Kafaka技术第一课
- Practical example of propeller easydl: automatic scratch recognition of industrial parts
- Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
- 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
- CMake教程Step5(添加系统自检)
- 深入理解Redis内存淘汰策略
猜你喜欢
【性能测试】jmeter+Grafana+influxdb部署实战
WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
NPM installation
Oracle缩表空间的完整解决实例
Embedded UC (UNIX System Advanced Programming) -2
thinkphp模板的使用
Example tutorial of SQL deduplication
基于51单片机的电子时钟设计
Redis+caffeine two-level cache enables smooth access speed
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
随机推荐
Tips for extracting JSON fields from MySQL
【testlink】TestLink1.9.18常见问题解决方法
Machine learning 01: Introduction
winedt常用快捷键 修改快捷键latex编译按钮
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
激动人心!2022开放原子全球开源峰会报名火热开启!
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
Practical example of propeller easydl: automatic scratch recognition of industrial parts
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
CMake教程Step4(安装和测试)
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
Judge whether a string is a full letter sentence
Use byte stream to read Chinese from file to console display
漫画:寻找无序数组的第k大元素(修订版)
腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
机器学习02:模型评估
WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
Embedded-c Language-4
Example tutorial of SQL deduplication
Wechat official account web page authorization login is so simple