当前位置:网站首页>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;
}
}
边栏推荐
- Dos:disk operating system, including core startup program and command program
- Use the data to tell you where is the most difficult province for the college entrance examination!
- 浅谈Multus CNI
- Check 15 developer tools of Alibaba
- libmysqlclient.so.20: cannot open shared object file: No such file or directory
- ASP. Net to access directory files outside the project website
- 【Day2】 convolutional-neural-networks
- SQL replying to comments
- MySQL case
- Golang defer
猜你喜欢
H5 audio tag custom style modification and adding playback control events
Some summaries of the third anniversary of joining Ping An in China
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
品牌连锁店5G/4G无线组网方案
Dynamic memory management
C语言指针面试题——第二弹
If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
Hands on deep learning (32) -- fully connected convolutional neural network FCN
智能网关助力提高工业数据采集和利用
2. Data type
随机推荐
有老师知道 继承RichSourceFunction自定义读mysql怎么做增量吗?
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
Today's sleep quality record 78 points
Golang Modules
Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
5g/4g wireless networking scheme for brand chain stores
Dynamic memory management
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
Native div has editing ability
Use C to extract all text in PDF files (support.Net core)
Summary of small program performance optimization practice
Exercise 7-2 finding the maximum value and its subscript (20 points)
C language pointer classic interview question - the first bullet
Some summaries of the third anniversary of joining Ping An in China
Go context basic introduction
H5 audio tag custom style modification and adding playback control events
C language pointer interview question - the second bullet
技术管理进阶——如何设计并跟进不同层级同学的绩效