这种题的鼻祖问题是schedule problem,也就是leetcode 646. Maximum Length of Pair Chain。 646.Maximum Length of Pair Chain,schedule:schedule问题的思想就是为了能有更多的interval,我们更关心的是interval结束的时间,越早结束代表着我们有更多interval的可能性更大,所以我们将intervals按照结束时间sort。 排序结束后,我们就要分析使用greedy进行分析:对于结束时间最早的元素,我们肯定要加到结果的count中;对于结束时间第二早的元素,如果他的开始时间大于第一个元素的结束时间,那么我们也加入结果count并且目前最后结束的时间是第二个元素的结束时间,如果不大于那么我们最后结束时间还是第一个元素的结束时间。依次类推,我们知道需要maintain一个endTime。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Solution { public int findLongestChain(int[][] pairs) { if(pairs.length==0) return 0; Arrays.sort(pairs, (a, b) -> { return a[1] - b[1]; }); int count = 1; int end = pairs[0][1]; for(int i =1;i<=pairs.length-1;i++){ if(pairs[i][0]>end){ count++; end = pairs[i][1]; } } return count; } }
class Solution { public int[][] merge(int[][] intervals) { if(intervals.length==0) return intervals; Arrays.sort(intervals, (a, b) -> { return a[0] - b[0]; });
List<int[]> ans = new LinkedList<>(); int start = intervals[0][0]; int end = intervals[0][1]; for(int i =1;i<=intervals.length-1;i++){ if(intervals[i][0]<=end){ end = Math.max(end, intervals[i][1]); } else { ans.add(new int[]{start, end}); start = intervals[i][0]; end = intervals[i][1]; } } ans.add(new int[]{start, end}); int[][] res = new int[ans.size()][2]; for(int i =0;i<=ans.size()-1;i++){ res[i] = ans.get(i); } return res; } }
/* 729. My Calendar I Medium 446 29 Favorite Share 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. Note: The number of calls to MyCalendar.book per test case will be at most 1000. In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9]. */
/* 731. My Calendar II Medium 444 67 Favorite Share Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking. Your class will have one 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 triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.) For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a triple 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(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. Note: The number of calls to MyCalendar.book per test case will be at most 1000. In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9]. */ classMyCalendarTwo{ List<int[]> calendar; List<int[]> overlap;
publicMyCalendarTwo(){ calendar = new LinkedList<>(); overlap = new LinkedList<>(); } publicbooleanbook(int start, int end){ for(int[] c:calendar){ if(Math.max(c[0], start)<Math.min(c[1], end)){ int overlapStart = Math.max(c[0], start); int overlapEnd = Math.min(c[1], end); for(int[] o:overlap){ if(Math.max(o[0], overlapStart)<Math.min(o[1], overlapEnd )){ overlap.clear(); returnfalse; } } overlap.add(newint[]{overlapStart, overlapEnd}); } } calendar.add(newint[]{start, end}); returntrue; } }
/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo obj = new MyCalendarTwo(); * boolean param_1 = obj.book(start,end); */