>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.
>//使用两个List保存区间的开始和结束时间点,每次添加一个事件,遍历已有List,查看是否有冲突, >//时间复杂度 O(n) >classMyCalendar{ privatefinal List<Integer> starts; privatefinal List<Integer> ends; publicMyCalendar(){ starts = new ArrayList<>(); ends = new ArrayList<>(); } publicbooleanbook(int start, int end){ for(int i = 0;i<starts.size();i++){ if(start >= ends.get(i) || end <= starts.get(i)){ continue; }else{ returnfalse; } } starts.add(start); ends.add(end); returntrue; } >} >//解法二,使用TreeMap保存已有的时间节点,利用二叉搜索树的性质来找到插入节点左右两边的节点,快速判断 >//时间复杂度: O(logN) >publicclassMyCalendar{ private TreeMap<Integer, Integer> map; publicMyCalendar(){ map = new TreeMap<>(); } publicbooleanbook(int start, int end){ Integer last = map.floorKey(start); Integer next = map.ceilingKey(start); if (last != null && next == null && start < map.get(last)) { returnfalse; } if (last == null && next != null && end > next) { returnfalse; } if (last != null && next != null && (start < map.get(last) || end > next)) { returnfalse; } map.put(start, end); returntrue; } >}
Calendar-II:
问题描述:
实现一个类似问题1中的日历功能,要求最多只能有一个冲突事件。
1 2 3 4 5 6 7 8 9 10 11 12 13
>MyCalendar(); >MyCalendar.book(10, 20); // returns true >MyCalendar.book(50, 60); // returns true >MyCalendar.book(10, 40); // returns true >MyCalendar.book(5, 15); // returns false >MyCalendar.book(5, 10); // returns true >MyCalendar.book(25, 55); // returns true >Explanation: >The first two events can be booked. The third event can be double booked. >The fourth event (5, 15) can't be booked, because it would result in a triple booking. >The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked. >The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event; >the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
>MyCalendarThree(); >MyCalendarThree.book(10, 20); // returns 1 >MyCalendarThree.book(50, 60); // returns 1 >MyCalendarThree.book(10, 40); // returns 2 >MyCalendarThree.book(5, 15); // returns 3 >MyCalendarThree.book(5, 10); // returns 3 >MyCalendarThree.book(25, 55); // returns 3 >Explanation: >The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking. >The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking. >The remaining events cause the maximum K-booking to be only a 3-booking. >Note that the last event locally causes a 2-booking, but the answer is still 3 because >eg. [10, 20), [10, 40), and [5, 15) are still triple booked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
>// 采用类似日历II中的区间冲突检测方法,直接返回最大的区间冲突次数即可。 >classMyCalendarThree{ privatefinal Map<Integer,Integer> map; publicMyCalendarThree(){ map = new TreeMap<>(); } publicintbook(int start, int end){ map.put(start,map.getOrDefault(start,0)+1); map.put(end,map.getOrDefault(end,0)-1); int cnt = 0; int max = Integer.MIN_VALUE; for(int c :map.values()){ cnt += c; max = Math.max(max,cnt); } return max; } >}