MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / MyCalendarTwo

Class MyCalendarTwo

MyCallendar2.java:2–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1//storing overlapped intervals
2class MyCalendarTwo {
3 List<int[]> bookings;
4 TreeMap<Integer,Integer> overlappedMap;
5 public MyCalendarTwo() {
6 bookings = new ArrayList<>();
7 overlappedMap = new TreeMap<>();
8 }
9
10 public boolean book(int start, int end) {
11 //if event is present in overlapped then return false
12 Integer prevVal = overlappedMap.lowerKey(end);
13 if(prevVal!=null && start <= overlappedMap.get(prevVal)-1){
14 return false;
15 }
16 // insert into bookings and if it is overalapping with
17 // any booking then insert into overlapped map
18 for(int booking[] : bookings){
19 // booking[0], start
20 // booking[1], end
21 int commStart = Math.max(booking[0],start);
22 int commEnd = Math.min(booking[1],end);
23 if(commStart<commEnd){
24 overlappedMap.put(commStart,commEnd);
25 }
26 }
27 bookings.add(new int[]{start,end});
28 return true;
29 }
30}
31
32/**
33 * Your MyCalendarTwo object will be instantiated and called as such:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected