figure set of nine events the earliest starting event begins at time the latest ending event ends at time the maximum number of concurrent events is { as well as others hintfocus on endpoints solutionthe number of events scheduled for given time changes only at times that are start or end times of an event this leads the following brute-force algorithm for each endpointcompute the number of events that contain it the maximum number of concurrent events is the maximum of this quantity over all endpoints if there are intervalsthe total number of endpoints is computing the number of events containing an endpoint takes (ntimesince checking whether an interval contains point takes ( time thereforethe overall time complexity is ( nxno( the inefficiency in the brute-force algorithm lies in the fact that it does not take advantage of localityi as we move from one endpoint to another intuitivelywe can improve the run time by sorting the set of all the endpoints in ascending order (if two endpoints have equal timesand one is start time and the other is an end timethe one corresponding to start time comes first if both are start or finish timeswe break ties arbitrarily now as we proceed through endpoints we can incrementally track the number of events taking place at that endpoint using counter for each endpoint that is the start of an intervalwe increment the counter by and for each endpoint that is the end of an intervalwe decrement the counter by the maximum value attained by the counter is maximum number of overlapping intervals for the example in figure the first seven endpoints are (start) (start) (start) (end) (end) (start) (endthe counter values are updated to event is tuple start_time end_time event collections namedtuple ('event '('start ''finish ')endpoint is tuple start_time or (end_time so that if times are equal start_time comes first endpoint collections namedtuple ('endpoint '('time ''is_start ')def find_max_simultaneous_events ( )builds an array of all endpoints (endpoint event start truefor event in aendpoint event finish false for event in ]sorts the endpoint array according to the time breaking ties by putting start times before end times sort(keylambda ( time not is_start )track the number of simultaneous events record the maximum number of simultaneous events max_num_simultaneous_events num_simultaneous_events for in