当前位置:网站首页>Force deduction solution summary 729- my schedule I
Force deduction solution summary 729- my schedule I
2022-07-22 19:32: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;
}
}
}
边栏推荐
- Force deduction solution summary 522- longest special sequence II
- Introduction to arrays
- Leetcode daily question 2021/12/20-2021/12/26
- Leetcode daily question 2022/3/14-2022/3/20
- LeetCode 每日一题 2021/12/20-2021/12/26
- This points to the problem
- 助力品牌洞察——消费者情绪行为分析
- 互联网通信安全之终端数据保护
- LeetCode 每日一题 2021/12/13-2021/12/19
- IT外包服务业各领域的未来前景和趋势
猜你喜欢
How to adjust the spacing and edge distance of MATLAB plot subgraph (solved)
“35岁,我退休了”:关于中年危机,这是最靠谱的回答
关于人力外包公司那些事
How can one page nest another page in thymeleaf? About page nesting, the tag tells you what you should know
Server operation and maintenance environment security system (Part I)
grafana面板-覆盖字段值
Day3:分支结构
Enumerate properties in objects
Oracle容器数据库的安装和使用
记一次 .NET 某RFID标签管理系统 CPU 暴涨分析
随机推荐
[FatFs] porting FatFs file system based on STM32 SD card
十年架构五年生活-05第一次出差
Help brand insight -- Analysis of consumers' emotional behavior
PHP implementation deletes a value in a one-dimensional array
Leetcode daily question 2022/3/28-2022/4/3
JS advanced - understanding of objects
Swagger-UI介绍及常用注解说明
Centos7 installs MySQL 5.7 decompressed version & Navicat connection MySQL & firewall settings - the personal test is valid
关于红队方面的学习资料
Matlab plot子图的间距和边缘距离如何调整(已解决)
Introduction to date object
MySQL optimization enforces the use of indexes
为什么重写equels方法一定要重写hashCode方法
Leetcode daily question 2022/2/21-2022/2/27
arguments
Rongyun x Xingzhi: exclusive Post-00 self social plot (including lottery)
JS advanced - basic data type and reference data type
Huawei mobile phone locking application
A practical example of the go pipeline pattern -- calculating the MD5 value of a series of files
LeetCode 每日一题 2021/12/6-2021/12/12