代码拉取完成,页面将自动刷新
from enum import Enum
class Data(object):
def __init__(self, timestamp, num_people, event_type):
self.timestamp = timestamp
self.num_people = num_people
self.event_type = event_type
def __lt__(self, other):
return self.timestamp < other.timestamp
class Period(object):
def __init__(self, start, end):
self.start = start
self.end = end
def __eq__(self, other):
return self.start == other.start and self.end == other.end
def __repr__(self):
return str(self.start) + ', ' + str(self.end)
class EventType(Enum):
ENTER = 0
EXIT = 1
class Solution(object):
def find_busiest_period(self, data):
if data is None:
raise TypeError('data cannot be None')
if not data:
return None
data.sort()
max_period = Period(0, 0)
max_people = 0
curr_people = 0
for index, interval in enumerate(data):
if interval.event_type == EventType.ENTER:
curr_people += interval.num_people
elif interval.event_type == EventType.EXIT:
curr_people -= interval.num_people
else:
raise ValueError('Invalid event type')
if (index < len(data) - 1 and
data[index].timestamp == data[index + 1].timestamp):
continue
if curr_people > max_people:
max_people = curr_people
max_period.start = data[index].timestamp
if index < len(data) - 1:
max_period.end = data[index + 1].timestamp
else:
max_period.end = data[index].timestamp
return max_period
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。