当前位置:网站首页>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;
}
}
边栏推荐
- 2021-08-11 function pointer
- How to teach yourself to learn programming
- 原生div具有编辑能力
- 基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
- 智能网关助力提高工业数据采集和利用
- 按键精灵跑商学习-商品数量、价格提醒、判断背包
- Hands on deep learning (39) -- gating cycle unit Gru
- 按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
- Hands on deep learning (43) -- machine translation and its data construction
- Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
猜你喜欢

C language pointer interview question - the second bullet

Log cannot be recorded after log4net is deployed to the server

Hands on deep learning (46) -- attention mechanism

Mmclassification annotation file generation

uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前

ASP. Net to access directory files outside the project website

C语言指针经典面试题——第一弹

Write a mobile date selector component by yourself

Reprint: summation formula of proportional series and its derivation process

El Table Radio select and hide the select all box
随机推荐
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Advanced technology management - how to design and follow up the performance of students at different levels
2021-08-10 character pointer
Exercise 9-5 address book sorting (20 points)
Sort out the power node, Mr. Wang he's SSM integration steps
Golang 类型比较
Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel
Modules golang
Hands on deep learning (38) -- realize RNN from scratch
Qtreeview+ custom model implementation example
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
On Multus CNI
Summary of reasons for web side automation test failure
Mmclassification annotation file generation
Fabric of kubernetes CNI plug-in
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
What are the advantages of automation?
Go context basic introduction
Today's sleep quality record 78 points
Hands on deep learning (46) -- attention mechanism