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)
  • 相关阅读:
    mybatis下使用log4j打印sql语句和执行结果
    chrome不支持embed标签解决方案
    在java中String类为什么要设计成final
    代理模式
    注解(二)模拟实体到数据库表字段的映射
    注解(一)
    python-redistest
    Agens层次聚类
    KNN近邻算法
    K-means聚类算法
  • 原文地址:https://www.cnblogs.com/liuqianli/p/8056779.html
Copyright © 2011-2022 走看看