732. My Calendar III

T: O(nlogn + n^2) = O(n^2), n times to call book()

S: O(n)

class MyCalendarThree {

    TreeMap<Integer, Integer> map;
    public MyCalendarThree() {
        map = new TreeMap<>();
    }
    
    public int book(int start, int end) {
        map.put(start, map.getOrDefault(start, 0) + 1);
        map.put(end, map.getOrDefault(end, 0) - 1);
        
        int max = 0;
        int count = 0;
        for (int value : map.values()) {
            count += value;
            max = Math.max(max, count);
        }
        return max;
    }
}

/**
這題就跟 meeting rooms 2 一樣解法...
 
 
 [[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
 
 
 
 10.  20.  
             50.  60
 10.      40
   5.15
   510
         25     55
 */

Last updated