当前位置:网站首页>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;
}
}
边栏推荐
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
- H5 audio tag custom style modification and adding playback control events
- How do microservices aggregate API documents? This wave of show~
- 智慧路灯杆水库区安全监测应用
- AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
- 按键精灵跑商学习-商品数量、价格提醒、判断背包
- 用数据告诉你高考最难的省份是哪里!
- Differences among opencv versions
- Exercise 9-5 address book sorting (20 points)
- Exercise 9-3 plane vector addition (15 points)
猜你喜欢
What are the advantages of automation?

Hands on deep learning (44) -- seq2seq principle and Implementation

Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment

Mmclassification annotation file generation

Regular expression (I)

Tables in the thesis of latex learning

leetcode1-3

用数据告诉你高考最难的省份是哪里!

C # use gdi+ to add text with center rotation (arbitrary angle)

Hands on deep learning (37) -- cyclic neural network
随机推荐
用数据告诉你高考最难的省份是哪里!
Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
Ruby时间格式转换strftime毫秒匹配格式
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
Kotlin set operation summary
Hands on deep learning (40) -- short and long term memory network (LSTM)
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
MATLAB小技巧(25)竞争神经网络与SOM神经网络
Go context 基本介绍
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
Devop basic command
Exercise 9-1 time conversion (15 points)
Matlab tips (25) competitive neural network and SOM neural network
Whether a person is reliable or not, closed loop is very important
Use C to extract all text in PDF files (support.Net core)
7-17 crawling worms (15 points)
Kotlin: collection use
Sort out the power node, Mr. Wang he's SSM integration steps
Uniapp--- initial use of websocket (long link implementation)
libmysqlclient.so.20: cannot open shared object file: No such file or directory