My Calendar III

class MyCalendarThree(object):
"""
Implement a MyCalendarThree class to store your events. A new event can always be added.
Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
A K-booking happens when K events have some non-empty intersection (ie., there is some time that is common to all K events.)
For each call to the method MyCalendar.book, return an integer K representing the largest integer such that there exists a K-booking in the calendar.
Your class will be called like this: MyCalendarThree cal = new MyCalendarThree(); MyCalendarThree.book(start, end)
"""

def __init__(self):
import collections
self.delta = collections.Counter()

def book(self, start, end):
"""
注意 这种方式 求的是里面重复的最大的次数,而不是最后一次加入的日历和前面重复的最大次数,
:param start:
:param end:
:return:
:param start:
:param end:
:return:
"""
self.delta[start] += 1
self.delta[end] -= 1

active = ans = 0
for x in sorted(self.delta):
if x > end:
break
active += self.delta[x]
if active > ans:
ans = active
print(active,ans)
print("-------")
return ans


s = MyCalendarThree()
r1 = s.book(1, 3)
r2 = s.book(2, 4)
r3 = s.book(5, 6)
# r4 = s.book(2, 7)
# r5 = s.book(5, 10)
# print(r1, r2, r3,r4,r5)
print(r3)
posted @ 2017-12-18 11:39  outback123  阅读(198)  评论(0编辑  收藏  举报