| Problem Names | Solution Writeups | Code |
|---|---|---|
| Insert Interval |
The rationale is to find the suitable position to insert and update the intervals. First, for every interval that is smaller than newInterval, simply add that interval to the answer vector. Then, when we finally reached the correct position to insert new interval, we create a while loop such that we keep updating new interval to min(intervals[i][0], newInterval[0]) and max(intervals[i][1], newInterval[1]). When we stop the while loop we then insert the newInterval into our answer vector. Finally insert all the remaining intervals. |
|
| Merge Intervals |
First loop from index 0 to second last index. If intervals[i] and intervals[i+1] has no intersection, simply add intervals[i] to ans vector, else update intervals[i+1][0] = intervals[i][0] and intervals[i+1][1] = max(intervals[i][1], intervals[i+1][1]). Remember to add the last interval to the answer vector once the loop is done. |
|
| Non Overlapping Intervals |
Loop from index 0 to second last. When we found that two intervals have intersection, greedily minimizing intervals[i+1] to minimize the number of intervals required to remove. |
|
| Meeting Rooms |
Sort the intervals array, and then check if any intervals[i][1] < intervals[i+1][0], simply returned false if this is found. Else returned true. |
|
| Meeting Rooms II |
In this problem, we need to count the number of rooms needed. The fastest way is to create two arrays, start and end, where start array will store the start time of all intervals in sorted order, and end array will store the end time in sorted order. Create two pointer i and j, if start[i] < end[j], increases i and j remains the same, else increase j and i remains the same. Once i is out of bound, the remaining end time in the end array will be the answer. |
|
| Minimum Interval to Include Each Query |
Sort intervals array, and create copy of queries array, together with its query index in a new array with element as pair(query, idx). Then sort the copied queries. After that, whenever we found a valid interval (that means query is in range), push it to a priority_queue with format (interval_size, right_limit of the interval). The interval size is used to determine the minimum interval while the right limit is used to determine if current interval is valid for the given query (since the interval can be from last query executed). |