【LeetCode】732. My Calendar III解题报告

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/my-calendar-iii/description/

题目描述:

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)

Example 1:
MyCalendarThree();
MyCalendarThree.book(10, 20); // returns 1
MyCalendarThree.book(50, 60); // returns 1
MyCalendarThree.book(10, 40); // returns 2
MyCalendarThree.book(5, 15); // returns 3
MyCalendarThree.book(5, 10); // returns 3
MyCalendarThree.book(25, 55); // returns 3
Explanation:
The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking.
The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.
The remaining events cause the maximum K-booking to be only a 3-booking.
Note that the last event locally causes a 2-booking, but the answer is still 3 because
eg. [10, 20), [10, 40), and [5, 15) are still triple booked.

Note:

  1. The number of calls to MyCalendarThree.book per test case will be at most 400.
  2. In calls to MyCalendarThree.book(start, end), start and end are integers in the range [0, 10^9].

解题方法

看这个:https://www.cnblogs.com/FannyChung/p/7896415.html

代码:

class Node(object):
def __init__(self, start, end, c):
self.start = start
self.end = end
self.count = c
self.left = None
self.right = None class MyCalendarThree(object):
def __init__(self):
self.root = None
self.maxK = 1 def book_helper(self, root, start, end, c):
if root == None:
return Node(start, end, c)
if start >= root.end:
#不能写成return self.boook_helper(),因为要进行树的构建和修改,一定要赋值给root.right
root.right = self.book_helper(root.right, start, end, c)
elif end <= root.start:
root.left = self.book_helper(root.left, start, end, c)
else:
intervals = sorted([start, end, root.start, root.end])
root_l, root_r = root.start, root.end
root.start, root.end = intervals[1], intervals[2]
root.left = self.book_helper(root.left, intervals[0], intervals[1], c if start <= root_l else root.count)
root.right = self.book_helper(root.right, intervals[2], intervals[3], c if end >= root_r else root.count)
root.count += c
self.maxK = max(root.count, self.maxK)
return root def book(self, start, end):
"""
:type start: int
:type end: int
:rtype: int
"""
self.root = self.book_helper(self.root, start, end, 1)
return self.maxK # Your MyCalendarThree object will be instantiated and called as such:
# obj = MyCalendarThree()
# param_1 = obj.book(start,end)

日期

2018 年 2 月 25 日

【LeetCode】732. My Calendar III解题报告的更多相关文章

  1. LeetCode 732. My Calendar III

    原题链接在这里:https://leetcode.com/problems/my-calendar-iii/ 题目: Implement a MyCalendarThree class to stor ...

  2. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  3. 【LeetCode】729. My Calendar I 解题报告

    [LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...

  4. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  5. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

随机推荐

  1. C语言按行读入文件

    getline() 函数无论一行多长,动态分配内存读入行 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <s ...

  2. jQuery添加html绑定事件

    jQuery添加html绑定事件 $("#xxx").on("click",".dev",function(){ });

  3. Python中类的各式方法介绍

    本文类的方法介绍包括类方法.属性方法.静态方法.修改属性方法等内置装饰器装饰的方法,以及类的一些特殊成员方法 1. 类的特殊成员方法 1.1 构造方法 # -*- coding:utf-8 -*- # ...

  4. C++类成员初始化列表的构造顺序

    看下面代码, 输出结果是多少呢? class A{ public: A(int k) : j(k), i(j) { } void show() { cout << this->i & ...

  5. echarts中饼状图数据太多进行翻页

    echarts饼状图数据太多 echarts 饼状图内容太多怎么处理 有些时候,我们饼状图中echarts的数据可能会很多. 这个时候展示肯定会密密麻麻的.导致显示很凌乱 我们需要'翻页'类似表格展示 ...

  6. 构建LNMP+WordPress

    1. 安装LNMP环境 首先修改主机名 [root@samba ~]# hostnamectl set-hostname lnmp[root@lnmp lnmp1.6-full]# hostnamec ...

  7. (数据科学学习手札132)Python+Fabric实现远程服务器连接

    本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 日常工作中经常需要通过SSH连接到多台远程 ...

  8. flink03-----1.Task的划分 2.共享资源槽 3.flink的容错

    1. Task的划分 在flink中,划分task的依据是发生shuffle(也叫redistrubute),或者是并行度发生变化 1.  wordcount为例 package cn._51doit ...

  9. DP-Burst Balloons

    leetcode312: https://leetcode.com/problems/burst-balloons/#/description Given n balloons, indexed fr ...

  10. ganglia -api

    setup 命令: virtualenv ve source ve/bin/activate pip install -r requirements.txt python ganglia/gangli ...