Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum no of overlaps of all time intervals [closed]

Tags:

algorithm

Given a set of time intervals , how to find the find the maximum no of overlaps . Is there any algorithm which solves the given problem with time complexity O(n log n ) or O(n)??

example : (6:00-9:30),(9:00-12:30),(10:00-10:30), (12:00-14:30), (11:00-13:30).The answer is 3

like image 680
user2601967 Avatar asked Dec 01 '22 18:12

user2601967


1 Answers

Sort the values using quick sort -- O(nlogn) time.

 6:00(+)
 9:30(-)
 9:00(+)
12:30(-)
10:00(+)
10:30(-)
12:14:30(Dude wut?) --> Im going to assume you meant 12:00(+) ,14:30(-)
11:00(+)
13:30(-)

Becomes

 6:00(+)
 9:00(+)
 9:30(-)
10:00(+)
10:30(-)
11:00(+)
12:00(+)
12:30(-)
13:30(-)
14:30(-)

Iterate through the list incrementing for every plus and decrementing for every minus, record the max value found. This takes O(n) time

Total time O(nlogn + n) = O(nlogn)

like image 133
gbtimmon Avatar answered Dec 04 '22 08:12

gbtimmon