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.

Articles by others on the same topic (0)

There are currently no matching articles.