Simple Java Sol based on TreeSet


  • 0
    R
    class MyCalendar {
    
        private Set<Interval> set;
    
        public MyCalendar() {
            set = new TreeSet<>((a, b) -> {
                if(a.end <= b.start) return -1;
                else if(a.start >= b.end) return 1;
                else return 0;
            });
        }
        
        public boolean book(int start, int end) {
            Interval interval = new Interval();
            interval.start = start;
            interval.end = end;
            if(set.contains(interval))
                return false;
            set.add(interval);
            return true;
        }
        
        private static class Interval {
            int start;
            int end;
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.