-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyCalendarI.java
More file actions
31 lines (24 loc) · 887 Bytes
/
MyCalendarI.java
File metadata and controls
31 lines (24 loc) · 887 Bytes
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
import java.util.TreeMap;
public class MyCalendarI {
static TreeMap<Integer,Integer> map;
MyCalendarI() {
map = new TreeMap<>();
}
public static void main(String[] args) {
MyCalendarI calendarI = new MyCalendarI();
System.out.println(calendarI.book(10,20));
System.out.println(calendarI.book(15,20));
}
public static boolean book(int left, int right) {
Integer low = map.lowerKey(right);
if(low == null || map.get(low) <= left) {
map.put(left,right);
return true;
}
return false;
}
}
//First implementation of using TreeMap
//TreeMap is not like HashMap, it does not using hashing function
//TreeMap is good to keep a sorted key value mapping. It sorts the map by the key
//Understanding Intervals is important. A lot of problems deal with intersecting intervals.