当前位置:网站首页>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
right1Compare , 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
endBe 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>e2perhapss2>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
边栏推荐
- Building the prototype of library functions -- refer to the manual of wildfire
- FTP server, ssh server (super brief)
- 有沒有sqlcdc監控多張錶 再關聯後 sink到另外一張錶的案例啊?全部在 mysql中操作
- HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
- Paper notes: graph neural network gat
- [untitled] a query SQL execution process in the database
- How to improve the level of pinduoduo store? Dianyingtong came to tell you
- 2020.02.11
- 【社区人物志】专访马龙伟:轮子不好用,那就自己造!
- [postgraduate entrance examination English] prepare for 2023, learn list5 words
猜你喜欢
![[robot library] awesome robots Libraries](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[robot library] awesome robots Libraries

Ue4- how to make a simple TPS role (II) - realize the basic movement of the role

Advanced technology management - what is the physical, mental and mental strength of managers

Online reservation system of sports venues based on PHP

一位博士在华为的22年

Black high-end responsive website dream weaving template (adaptive mobile terminal)

Computer graduation design PHP campus restaurant online ordering system

在线怎么生成富文本

Derivation of Biot Savart law in College Physics

Concept of storage engine
随机推荐
Global and Chinese market of wheelchair climbing machines 2022-2028: Research Report on technology, participants, trends, market size and share
After changing the GCC version, make[1] appears in the compilation: cc: command not found
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
Use Scrollview and tabhost to realize vertical scrollbars and tabs
Reset nodejs of the system
Minecraft 1.16.5 biochemical 8 module version 2.0 storybook + more guns
Jisuanke - t2063_ Missile interception
Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
729. 我的日程安排表 I / 剑指 Offer II 106. 二分图
Ue4- how to make a simple TPS role (II) - realize the basic movement of the role
我把驱动换成了5.1.35,但是还是一样的错误,我现在是能连成功,但是我每做一次sql操作都会报这个
[solution] every time idea starts, it will build project
Global and Chinese market of commercial cheese crushers 2022-2028: Research Report on technology, participants, trends, market size and share
Easy to use js script
[robot library] awesome robots Libraries
Executing two identical SQL statements in the same sqlsession will result in different total numbers
sql表名作为参数传递
MySQL winter vacation self-study 2022 11 (6)
How to use C to copy files on UNIX- How can I copy a file on Unix using C?
剑指 Offer 29. 顺时针打印矩阵