当前位置:网站首页>Li Kou today's question -729 My schedule I
Li Kou today's question -729 My schedule I
2022-07-06 02:26:00 【Struggling young man】
729. My schedule I
Two points
- First, sort all the scheduled sequences , Then traverse the interval in turn , Look at a certain interval
left1
, Is it greater thanend
12 Hui He 3,5,10 Compare , It will be found that there is no larger range , It's time to consider inserting them behind .
- Not found : That's the same as the last interval
right1
Compare , Look at the last interval right1 Whether it is smaller than the insertion interval start, If you can, return to ture, Otherwise it returns false
12 And the last element 15 Compare , Find out 15 It is greater than 12 Of , return false.
and 17 The first and 3,7,10 Compare , Found that they are bigger than them , So consider putting it back , then 20 and 15 Compare , Find out 20 Greater than 15, return true.
- Judge to find the first
I. judgment , Found
end
Be smaller than the smallestleft1
, So you can insert , Just go back to true
- Intermediate insertion
Intermediate interval , find last The last interval of pre[left2,right2]
, if right2 Be greater than start, Then there is intersection , return false, Otherwise return to true.
First judge , Find out 10==10, Stop and judge 7, Take a look 7<8 There is intersection , return false.
such as [9,10] This is a correct example , Find out 10 =- 10, Stop and judge , Take a look 9> 8, return true.
- Code :
List<int[]> data;
public MyCalendar() {
data = new ArrayList<>();
}
public boolean book(int start, int end) {
if (data.size() == 0) {
data.add(new int[]{
start, end});
return true;
}
data.sort(Comparator.comparingInt(o -> o[0]));
int left = 0, right = data.size() - 1;
// Find the first start >= end The range of
int ans = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (data.get(mid)[0] >= end) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
// Can't find Compare with the last interval
if (ans == -1) {
int[] ints = data.get(data.size() - 1);
if (ints[1] > start) {
return false;
}
} else if (ans != 0) {
// Not the first interval , If it is the first interval, you can directly insert
int[] pre = data.get(ans - 1);
if (pre[1] > start) {
return false;
}
}
data.add(new int[]{
start, end});
return true;
}
Direct traversal
class MyCalendar {
// Define a booked
List<int[]> booked;
public MyCalendar() {
booked = new ArrayList<int[]>();
}
public boolean book(int start, int end) {
for(int[] arr : booked){
int l = arr[0] ,r = arr[1];
// The second case
if(l < end && start < r){
return false;
}
}
booked.add(new int[]{
start,end});
return true;
}
}
/** * Your MyCalendar object will be instantiated and called as such: * MyCalendar obj = new MyCalendar(); * boolean param_1 = obj.book(start,end); */
[s1,e1) and [s2,e2)
, No intersection is satisfieds1>e2
perhapss2>e1
, On the contrary, it means that the two return to produce intersection .
Train of thought three
Change your mind , The concept of overlap can be extended to the generalized concept of equality , That is, once overlapped, it means that the two elements are equal , Then it's simple ,set Specially used for de duplication storage . that HashSet and TreeSet Which one to use ? First HashSet De duplication is based on the hash value of the object , Because what we want to store is int[] Type of , When we write code, we should base on start and end To create it , Then their hash values are basically different , That is to say, even two int[] The values inside are exactly the same , But the hash value is different ,HashSet They will also be stored . Because we regard overlap as equality in a broad sense , Then we need to define a comparator to distinguish whether their generalized equality , and TreeSet Support custom comparator , And sort according to the value returned by the comparator , Very much in line with our needs .
class MyCalendar {
// Define a TreeSet
TreeSet<int[]> calendars;
public MyCalendar() {
calendars = new TreeSet<>((a, b) -> {
if(a[1] <= b[0])
return -1;
else if(a[0] >= b[1])
return 1;
else
return 0;
});
}
public boolean book(int start, int end) {
int[] e = new int[]{
start, end};
return calendars.add(e);
}
}
/** * Your MyCalendar object will be instantiated and called as such: * MyCalendar obj = new MyCalendar(); * boolean param_1 = obj.book(start,end); */
expand
TreeSet brief introduction :
Orderly , Do not repeat , Red and black trees , be based on Treemap Realization , Custom sorting and other features .
TreeSet It's an ordered set , Its function is to provide order Set aggregate . It is inherited from AbstractSet abstract class , Realized NavigableSet, Cloneable, java.io.Serializable Interface .
TreeSet Inherited from AbstractSet, So it's a Set aggregate , have Set Properties and methods of .
TreeSet Realized NavigableSet Interface , It means that it supports a range of navigation methods . For example, find the best match with the specified target .
TreeSet Realized Cloneable Interface , It means it can be cloned .
TreeSet Realized java.io.Serializable Interface , Means it supports serialization .
TreeSet Is based on TreeMap Realized .TreeSet The elements in support 2 Sort by : Natural ordering perhaps According to create TreeSet Provided by Comparator Sort . It depends on the construction method used .
TreeSet For basic operation (add、remove and contains) Provide guaranteed log(n) Time cost .
in addition ,TreeSet asynchronous . its iterator The iterator returned by the method is fail-fast Of
边栏推荐
- Know MySQL database
- 2022 edition illustrated network pdf
- PAT甲级 1033 To Fill or Not to Fill
- 0211 embedded C language learning
- 数据工程系列精讲(第四讲): Data-centric AI 之样本工程
- [untitled] a query SQL execution process in the database
- 技术管理进阶——什么是管理者之体力、脑力、心力
- Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
- Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械
- [Wu Enda machine learning] week5 programming assignment EX4 - neural network learning
猜你喜欢
Virtual machine network, networking settings, interconnection with host computer, network configuration
[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
【机器人库】 awesome-robotics-libraries
力扣今日题-729. 我的日程安排表 I
Minecraft 1.16.5 biochemical 8 module version 2.0 storybook + more guns
Know MySQL database
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
RDD partition rules of spark
Social networking website for college students based on computer graduation design PHP
随机推荐
大厂镜像库
HDU_ p1237_ Simple calculator_ stack
ftp上传文件时出现 550 Permission denied,不是用户权限问题
论文笔记: 极限多标签学习 GalaXC (暂存, 还没学完)
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
好用的 JS 脚本
从顶会论文看2022年推荐系统序列建模的趋势
3D drawing ()
Pangolin Library: subgraph
Global and Chinese markets hitting traffic doors 2022-2028: Research Report on technology, participants, trends, market size and share
有没有sqlcdc监控多张表 再关联后 sink到另外一张表的案例啊?全部在 mysql中操作
Black high-end responsive website dream weaving template (adaptive mobile terminal)
A doctor's 22 years in Huawei
550 permission denied occurs when FTP uploads files, which is not a user permission problem
Have a look at this generation
Using SA token to solve websocket handshake authentication
Shell脚本更新存储过程到数据库
SQL table name is passed as a parameter
机器学习训练与参数优化的一般过程 (讨论)
sql表名作为参数传递