Solution

ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/functional-programming-design-and-analysis-of-algorithms/8/e/solution

Sort once by nondecreasing end time, then retain the most recent meeting:
Meetings(intervals):
    sort intervals by nondecreasing end
    result = []
    haveMeeting = false
    for (s,e) in intervals:
        if not haveMeeting or s > lastMeeting:
            lastMeeting = e
            append e to result
            haveMeeting = true
    return result
When an interval is inspected, because of the sorted end order. Therefore the interval is already covered exactly when ; otherwise it is the earliest-ending uncovered interval and the algorithm chooses its end. This proves that the scan implements greedy strategy 2. The strict test s > lastMeeting correctly treats equality as covered for closed intervals.
The sorting costs comparisons, and the scan costs , with space for the output and constant scan state. Including an empty input, the overall bound is time, conventionally for . Additional sorting space depends on the selected sorting algorithm. Part (d) proves that the returned number of meetings is minimum.

New to topics? Read the docs here!