Leectcode732. 我的日程安排表 III python实现

一.题目描述

Leectcode732. 我的日程安排表 III:

      实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排。MyCalendar 有一个 book(int start, int end)方法。它意味着在start到end时间内增加一个日程安排,注意,这里的时间是半开区间,即 [start, end), 实数 x 的范围为,  start <= x < end

K 个日程安排有一些时间上的交叉时(例如K个日程安排都在同一时间内),就会产生 K 次预订。

每次调用 MyCalendar.book方法时,返回一个整数 K ,表示最大的 K 次预订。

请按照以下步骤调用MyCalendar 类: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)

二.示例

Leectcode732. 我的日程安排表 III python实现

三.算法分析

   假设只考虑start和end,利用数据结构图计算节点的出度就是最大预订次数。

四.算法实现

import collections


class MyCalendarThree:

    def __init__(self):

        #构建字典对象
        self.book_dict = collections.defaultdict(int)

    def book(self, start: 'int', end: 'int') -> 'int':
        self.book_dict[start]+=1
        self.book_dict[end]-=1
        outdegree = 0
        max = 0

        #计算出度则为最大预订次数
        for i,j in sorted(self.book_dict.items()):
            outdegree+=j
            if max < outdegree:
                max=outdegree
        return max
        

# Your MyCalendarThree object will be instantiated and called as such:
# obj = MyCalendarThree()
# param_1 = obj.book(start,end)

五.执行结果

Leectcode732. 我的日程安排表 III python实现