forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCalendar1.java
59 lines (51 loc) · 1.27 KB
/
MyCalendar1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*https://leetcode.com/problems/my-calendar-i/*/
class Event implements Comparable<Event>
{
int startTime, endTime;
Event(int s, int e)
{
this.startTime = s;
this.endTime = e;
}
@Override
public int compareTo(Event other)
{
return this.endTime-other.endTime;
}
@Override
public String toString()
{
return "("+this.startTime+"->"+this.endTime+")";
}
}
class MyCalendar {
List<Event> events;
public MyCalendar() {
events = new ArrayList<Event>();
}
public boolean book(int start, int end) {
for (Event event : events)
{
if (start < event.endTime && end > event.startTime)
return false;
}
events.add(new Event(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);
*/
class MyCalendar {
private TreeMap<Integer, Integer> map = new TreeMap<>();
public MyCalendar() {
}
public boolean book(int start, int end) {
Map.Entry<Integer, Integer> e = map.floorEntry(end - 1);
if (e != null && e.getValue() > start) return false;
map.put(start, end);
return true;
}
}