|
| 1 | +package LeetCodeJava.Array; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/my-calendar-ii/description/ |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.TreeMap; |
| 9 | + |
| 10 | +/** |
| 11 | + * 731. My Calendar II |
| 12 | + * Medium |
| 13 | + * Topics |
| 14 | + * Companies |
| 15 | + * Hint |
| 16 | + * You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking. |
| 17 | + * |
| 18 | + * A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.). |
| 19 | + * |
| 20 | + * The event can be represented as a pair of integers startTime and endTime that represents a booking on the half-open interval [startTime, endTime), the range of real numbers x such that startTime <= x < endTime. |
| 21 | + * |
| 22 | + * Implement the MyCalendarTwo class: |
| 23 | + * |
| 24 | + * MyCalendarTwo() Initializes the calendar object. |
| 25 | + * boolean book(int startTime, int endTime) Returns 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. |
| 26 | + * |
| 27 | + * |
| 28 | + * Example 1: |
| 29 | + * |
| 30 | + * Input |
| 31 | + * ["MyCalendarTwo", "book", "book", "book", "book", "book", "book"] |
| 32 | + * [[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]] |
| 33 | + * Output |
| 34 | + * [null, true, true, true, false, true, true] |
| 35 | + * |
| 36 | + * Explanation |
| 37 | + * MyCalendarTwo myCalendarTwo = new MyCalendarTwo(); |
| 38 | + * myCalendarTwo.book(10, 20); // return True, The event can be booked. |
| 39 | + * myCalendarTwo.book(50, 60); // return True, The event can be booked. |
| 40 | + * myCalendarTwo.book(10, 40); // return True, The event can be double booked. |
| 41 | + * myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking. |
| 42 | + * myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked. |
| 43 | + * myCalendarTwo.book(25, 55); // return True, The event 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. |
| 44 | + * |
| 45 | + * |
| 46 | + * Constraints: |
| 47 | + * |
| 48 | + * 0 <= start < end <= 109 |
| 49 | + * At most 1000 calls will be made to book. |
| 50 | + * Seen this question in a real interview before? |
| 51 | + * 1/5 |
| 52 | + * Yes |
| 53 | + * No |
| 54 | + * Accepted |
| 55 | + * 187.4K |
| 56 | + * Submissions |
| 57 | + * 298.6K |
| 58 | + * Acceptance Rate |
| 59 | + * 62.8% |
| 60 | + */ |
| 61 | +public class MyCalendar2 { |
| 62 | + |
| 63 | + /** |
| 64 | + * Your MyCalendarTwo object will be instantiated and called as such: |
| 65 | + * MyCalendarTwo obj = new MyCalendarTwo(); |
| 66 | + * boolean param_1 = obj.book(startTime,endTime); |
| 67 | + */ |
| 68 | + |
| 69 | + // V0 |
| 70 | +// class MyCalendarTwo { |
| 71 | +// |
| 72 | +// public MyCalendarTwo() { |
| 73 | +// |
| 74 | +// } |
| 75 | +// |
| 76 | +// public boolean book(int startTime, int endTime) { |
| 77 | +// |
| 78 | +// } |
| 79 | +// } |
| 80 | + |
| 81 | + // V1-1 |
| 82 | + // https://leetcode.com/problems/my-calendar-ii/editorial/ |
| 83 | + // IDEA: Using Overlapped Intervals |
| 84 | + class MyCalendarTwo_1_1 { |
| 85 | + |
| 86 | + private List<int[]> bookings; |
| 87 | + private List<int[]> overlapBookings; |
| 88 | + |
| 89 | + // Return true if the booking [start1, end1) & [start2, end2) overlaps. |
| 90 | + private boolean doesOverlap(int start1, int end1, int start2, int end2) { |
| 91 | + return Math.max(start1, start2) < Math.min(end1, end2); |
| 92 | + } |
| 93 | + |
| 94 | + // Return overlapping booking between [start1, end1) & [start2, end2). |
| 95 | + private int[] getOverlapped(int start1, int end1, int start2, int end2) { |
| 96 | + return new int[] { Math.max(start1, start2), Math.min(end1, end2) }; |
| 97 | + } |
| 98 | + |
| 99 | + public MyCalendarTwo_1_1() { |
| 100 | + bookings = new ArrayList<>(); |
| 101 | + overlapBookings = new ArrayList<>(); |
| 102 | + } |
| 103 | + |
| 104 | + public boolean book(int start, int end) { |
| 105 | + // Returns false if the new booking has an overlap |
| 106 | + // with the existing double-booked bookings. |
| 107 | + for (int[] booking : overlapBookings) { |
| 108 | + if (doesOverlap(booking[0], booking[1], start, end)) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Add the double overlapping bookings if any with the new booking. |
| 114 | + for (int[] booking : bookings) { |
| 115 | + if (doesOverlap(booking[0], booking[1], start, end)) { |
| 116 | + overlapBookings.add( |
| 117 | + getOverlapped(booking[0], booking[1], start, end)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Add the new booking to the list of bookings. |
| 122 | + bookings.add(new int[] { start, end }); |
| 123 | + return true; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + // V1-2 |
| 129 | + // https://leetcode.com/problems/my-calendar-ii/editorial/ |
| 130 | + // IDEA: Line Sweep |
| 131 | + class MyCalendarTwo_1_2 { |
| 132 | + |
| 133 | + private TreeMap<Integer, Integer> bookingCount; |
| 134 | + private int maxOverlappedBooking; |
| 135 | + |
| 136 | + public MyCalendarTwo_1_2() { |
| 137 | + bookingCount = new TreeMap<>(); |
| 138 | + maxOverlappedBooking = 2; |
| 139 | + } |
| 140 | + |
| 141 | + public boolean book(int start, int end) { |
| 142 | + // Increase the booking count at 'start' and decrease at 'end'. |
| 143 | + bookingCount.put(start, bookingCount.getOrDefault(start, 0) + 1); |
| 144 | + bookingCount.put(end, bookingCount.getOrDefault(end, 0) - 1); |
| 145 | + |
| 146 | + int overlappedBooking = 0; |
| 147 | + |
| 148 | + // Calculate the prefix sum of bookings. |
| 149 | + for (Map.Entry<Integer, Integer> entry : bookingCount.entrySet()) { |
| 150 | + overlappedBooking += entry.getValue(); |
| 151 | + |
| 152 | + // If the number of overlaps exceeds the allowed limit, rollback and |
| 153 | + // return false. |
| 154 | + if (overlappedBooking > maxOverlappedBooking) { |
| 155 | + // Rollback changes. |
| 156 | + bookingCount.put(start, bookingCount.get(start) - 1); |
| 157 | + bookingCount.put(end, bookingCount.get(end) + 1); |
| 158 | + |
| 159 | + // Clean up if the count becomes zero to maintain the map clean. |
| 160 | + if (bookingCount.get(start) == 0) { |
| 161 | + bookingCount.remove(start); |
| 162 | + } |
| 163 | + |
| 164 | + return false; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return true; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + // V2 |
| 174 | +} |
0 commit comments