zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    Canvas基础讲义
    封装一个DivTag
    递归深拷贝
    构造函数的执行过程
    封装一个Ajax工具函数
    数组去重
    [js开源组件开发]js多选日期控件
    自己写的表格插件autotable
    复杂表格的树形结构的添加删除行div实现
    自制html5塔防游戏
  • 原文地址:https://www.cnblogs.com/liuqianli/p/8056779.html
Copyright © 2011-2022 走看看