729. My Calendar I

Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.
Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)
For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
Example 1:

MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
Explanation: 
The first event can be booked.  The second can't because time 15 is already booked by another event.
The third event can be booked, as the first event takes every time less than 20, but not including 20.




Solution 1 : 
Time : 1 + 2 + 3 + .. + n = (1 + n) * ( n / 2) = O(n ^ 2)
Or think this way . when the size of the booked is n, we need to 
Compare the new query date with the ones in the booked already, so that’s n
times. and we need to do this n times , since we have n calls for book() 
So n * n 

Space : O(n) 

interval [ s1, e1) and [s2, e2) overlap when 
max(s1, s2) < min(e1, e2)



class MyCalendar {

    private List<int[]> booked;
      

    public MyCalendar() {
        booked = new ArrayList<>();
        
    }
    
    public boolean book(int start, int end) {
        for(int[] pair : booked){
            int s = pair[0];
            int e = pair[1];
            if(Math.max(s, start) < Math.min(e, end)) return false;
        }
        booked.add(new int[]{start, end});
        return true;
    }
}





Solution 2 : 
Tree map can be implemented by using balanced binary tree , red black tree ? 

The floorKey(K key) method is used to return the greatest key less than or equal to the given key, or null if there is no such key.


calendar.get(prev) , this is to get the end of the prev 

Also, no need to worry about cases like 
     ————
———————    new query(here) 

this the intervals in the list is not overlapped , so this case is not 
Gonna happen , and when we want to get the floorKey(key), we are sure 
That the prev one is the closest to the the new query’s pos 

prev query ,  new query,  next query

So when we check if the new query fits in the current pool of queries
we need to check two things, prev query, and next query. Since it’s sorted. 
Check if prev query. End < new query.start 

And next query.start > new query.end

If both conditions satisfies, then overlap exists and we can put the new
Query into the tree map and return true
Else return false 


* Time Complexity (Java): O(N \log N)

O(NlogN), where N is the number of events booked. For each new event, we search that the event is legal in O(\log N)

O(logN) time, then insert it in O(1)

O(1) time.

* Time Complexity (Python): O(N^2)
 worst case, with O(N \log N)

O(NlogN) on random data. For each new event, we insert the event into our binary tree. As this tree may not be balanced, it may take a linear number of steps to add each event.

* Space Complexity: O(N)

O(N), the size of the data structures used.







class MyCalendar {
    TreeMap<Integer, Integer> booked;
    // key is start time, value is end time 

    public MyCalendar() {
      booked = new TreeMap();
        
    }
    
    public boolean book(int start, int end) {
      Integer prevStart = booked.floorKey(start);
      Integer nextStart = booked.ceilingKey(start);
      
      if((prevStart == null || booked.get(prevStart) <= start) && 
         (nextStart == null || end <= nextStart)){
        booked.put(start, end);
        return true;
      }
      return false;    
    }
}

 

posted on 2018-09-20 18:27  猪猪&#128055;  阅读(151)  评论(0)    收藏  举报

导航