-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintervals_overlap.py
executable file
·86 lines (68 loc) · 2.13 KB
/
intervals_overlap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
# vim: foldlevel=0
"""
Consider a big party where a log register for guest's entry and exit times is maintained.
Find the time at which there are maximum guests in the party. Note that entries in
register are not in any order.
Example:
Input: arrl[] = {1, 2, 9, 5, 5}
exit[] = {4, 5, 12, 9, 12}
First guest in array arrives at 1 and leaves at 4, second guest arrives at 2 and
leaves at 5, and so on.
Output: 5
There are maximum 3 guests at time 5.
http://www.geeksforgeeks.org/find-the-point-where-maximum-intervals-overlap/
"""
def solution1(arrl, exit):
"""
Time complexity: O(k*n) where k is average interval length and n is number of
intervals.
>>> solution1([1, 2, 9, 5, 5], [4, 5, 12, 9, 12])
5
"""
min_arrl, max_exit = min(arrl), max(exit)
# Allocate count array
count = []
for i in range(max_exit - min_arrl + 1):
count.append(0)
# Traverse all intervals and increment count accordingly
for interval in zip(arrl, exit):
for i in range(interval[0], interval[1]+1):
count[i-min_arrl] += 1 # offset min_arr
# Find max count
maxidx = 0
for i in range(len(count)):
if count[i] > count[maxidx]:
maxidx = i
return min_arrl + maxidx
# Alternatively:
#return min_arrl+next(i for i, c in enumerate(count) if c == max(count))
def solution2(arrl, exit):
"""
Time complexity: O(nlog(n)) for the sorting
Traverse both arrival and exit lists in parallel using merge portion of mergesort
and keep track of max number of overlap.
>>> solution2([1, 2, 9, 5, 5], [4, 5, 12, 9, 12])
5
"""
arrl.sort()
exit.sort()
# 1, 2, 5, 5, 9
# 4, 5, 9, 12, 12
i, j = 0, 0
count, maxcount = 0, 0
res = 0
while i < len(arrl) and j < len(exit):
if arrl[i] <= exit[j]:
count += 1
if count > maxcount:
maxcount = count
res = arrl[i]
i += 1
else:
count -= 1
j += 1
return res
if __name__ == "__main__":
import doctest
doctest.testmod()