当前位置:网站首页>leetcode729. My schedule 1
leetcode729. My schedule 1
2022-07-04 10:16:00 【JoesonChan】
Problem description
Achieve one MyCalendar Class to store your schedule . If there is no other schedule for the time to be added , You can store this new schedule .
MyCalendar There is one book(int start, int end) Method . It means in start To end Add a schedule in time , Be careful , The time here is a half open interval , namely [start, end), The set of real Numbers x For the range of , start <= x < end.
When there is some time overlap between the two schedules ( For example, both schedules are in the same time ), There will be repeat bookings .
Every time you call MyCalendar.book When the method is used , 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 .
Please follow the steps below to call MyCalendar class : MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
Example 1:
MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
explain :
The first schedule can be added to the calendar .
The second schedule cannot be added to the calendar , Because of time 15 It has been scheduled by the first schedule .
The third schedule can be added to the calendar , Because the first schedule doesn't include time 20 .
explain :
Each test case , call MyCalendar.book The function is no more than 100 Time .
Call function MyCalendar.book(start, end) when , start and end The value range of is [0, 10^9].
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class MyCalendar1 {
TreeMap<Integer, Integer> treeMap = null;
public MyCalendar1() {
treeMap = new TreeMap<>(Comparator.naturalOrder());
}
public boolean book(int start, int end) {
if (end <= start) {
return false;
}
for (Map.Entry<Integer, Integer> entry : treeMap.entrySet()) {
if (start >= entry.getValue()) {
break;
}
if(end <= entry.getKey()){
continue;
}
if (start < entry.getValue() || end > entry.getKey()) {
return false;
}
}
treeMap.put(start, end);
return true;
}
}
边栏推荐
- xxl-job惊艳的设计,怎能叫人不爱
- Tables in the thesis of latex learning
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- Legion is a network penetration tool
- Container cloud notes
- 今日睡眠质量记录78分
- Kubernetes CNI 插件之Fabric
- How to teach yourself to learn programming
- C # use gdi+ to add text with center rotation (arbitrary angle)
- Hands on deep learning (35) -- text preprocessing (NLP)
猜你喜欢
Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
Hands on deep learning (44) -- seq2seq principle and Implementation
How can Huawei online match improve the success rate of player matching
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
C语言指针经典面试题——第一弹
Application of safety monitoring in zhizhilu Denggan reservoir area
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
Matlab tips (25) competitive neural network and SOM neural network
libmysqlclient.so.20: cannot open shared object file: No such file or directory
Qtreeview+ custom model implementation example
随机推荐
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
For programmers, if it hurts the most...
Dos:disk operating system, including core startup program and command program
C # use smtpclient The sendasync method fails to send mail, and always returns canceled
Histogram equalization
Legion is a network penetration tool
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
Summary of small program performance optimization practice
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
Kotlin: collection use
PHP代码审计3—系统重装漏洞
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
C # use gdi+ to add text with center rotation (arbitrary angle)
Check 15 developer tools of Alibaba
SQL replying to comments
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
How can people not love the amazing design of XXL job
Tables in the thesis of latex learning
[FAQ] summary of common causes and solutions of Huawei account service error 907135701