当前位置:网站首页>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;
}
}
边栏推荐
- JDBC and MySQL database
- Use C to extract all text in PDF files (support.Net core)
- Vs201 solution to failure to open source file HPP (or link library file)
- Golang 类型比较
- System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
- Golang defer
- uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
- 按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
- C语言指针经典面试题——第一弹
- Dynamic memory management
猜你喜欢
How do microservices aggregate API documents? This wave of show~
【Day1】 deep-learning-basics
Hands on deep learning (33) -- style transfer
Fabric of kubernetes CNI plug-in
5g/4g wireless networking scheme for brand chain stores
Tables in the thesis of latex learning
Hands on deep learning (32) -- fully connected convolutional neural network FCN
Summary of reasons for web side automation test failure
Reasons and solutions for the 8-hour difference in mongodb data date display
How can Huawei online match improve the success rate of player matching
随机推荐
View CSDN personal resource download details
使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
Golang Modules
What is devsecops? Definitions, processes, frameworks and best practices for 2022
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
PHP code audit 3 - system reload vulnerability
技术管理进阶——如何设计并跟进不同层级同学的绩效
Tables in the thesis of latex learning
2020-03-28
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
C # use smtpclient The sendasync method fails to send mail, and always returns canceled
智能网关助力提高工业数据采集和利用
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
直方图均衡化
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
H5 audio tag custom style modification and adding playback control events
Exercise 8-10 output student grades (20 points)
xxl-job惊艳的设计,怎能叫人不爱