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)
  • 相关阅读:
    真爱 vs. 种姓:新一代印度人的婚恋观
    美国司法部解禁guns打印技术
    特朗普访英,吃瓜群众却只想看《真爱至上》
    Semaphore(信号量)
    RLock(递归锁)
    用python编写九九乘法表
    php传值和传引用的区别
    post请求的header
    Content-type详解
    thinkphp5 学习笔记
  • 原文地址:https://www.cnblogs.com/liuqianli/p/8056779.html
Copyright © 2011-2022 走看看