• 2022-6-24 我的日程安排表II,掉落的方块


    1. 我的日程安排表 II

    You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.

    A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).

    The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.

    Implement the MyCalendarTwo class:

    • MyCalendarTwo() Initializes the calendar object.
    • boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.

    Example

    Input
    ["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
    [[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
    Output
    [null, true, true, true, false, true, true]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    代码

    class MyCalendarTwo:
    
        def __init__(self):
            self.tree = collections.defaultdict(int)
            self.lazy = collections.defaultdict(int)
    
        def query(self, start: int, end: int, l: int, r: int, idx: int) -> int:
            if r < start or end < l: return 0
            if start <= l and r <= end: return self.tree[idx]
            m = (l + r) >> 1
            l_m = self.query(start, end, l, m, (idx << 1) + 1)
            r_m = self.query(start, end, m + 1, r, (idx << 1) + 2)
            return max(l_m, r_m) + self.lazy[idx]
    
        def update(self, start: int, end: int, l: int, r: int, idx: int):
            if r < start or end < l: return
            if start <= l and r <= end:
                self.tree[idx] += 1
                self.lazy[idx] += 1
            else:
                m = (l + r) >> 1
                self.update(start, end, l, m, (idx << 1) + 1)
                self.update(start, end, m + 1, r, (idx << 1) + 2)
                self.tree[idx] = self.lazy[idx] + max(self.tree[(idx << 1) + 1], self.tree[(idx << 1) + 2])
    
        def book(self, start: int, end: int) -> bool:
            if self.query(start, end - 1, 0, int(10 ** 9), 0) >= 2: return False
            self.update(start, end - 1, 0, int(10 ** 9), 0)
            return True
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2. 掉落的方块

    There are several squares being dropped onto the X-axis of a 2D plane.

    You are given a 2D integer array positions where positions[i] = [lefti, sideLengthi] represents the ith square with a side length of sideLengthi that is dropped with its left edge aligned with X-coordinate lefti.

    Each square is dropped one at a time from a height above any landed squares. It then falls downward (negative Y direction) until it either lands on the top side of another square or on the X-axis. A square brushing the left/right side of another square does not count as landing on it. Once it lands, it freezes in place and cannot be moved.

    After each square is dropped, you must record the height of the current tallest stack of squares.

    Return an integer array ans where ans[i] represents the height described above after dropping the ith square.

    Example

    输入:positions = [[1,2],[2,3],[6,1]]
    输出:[2,5,5]
    
    • 1
    • 2

    代码

    
    
    • 1
  • 相关阅读:
    GEE案例——如何进行重采样(分辨率由高分辨率降为低分辨率)以sentinel2为例重采样到1000米
    数据压缩和归档(二)、zipfile
    【web-攻击后端组件】(7.2)操作文件路径、注入XML解释器、注入后端HTTP请求、注入电子邮件
    2021年03月 Python(三级)真题解析#中国电子学会#全国青少年软件编程等级考试
    SpringMVC学习|JSON讲解、Controller返回JSON数据、Jackson、JSON乱码处理、FastJson
    locust性能压测工具
    MS2358:96KHz、24bit 音频 ADC
    如何让Java项目兼容更多的客户端设备(二)
    uni-app进行表单效验
    【Python】正则表达式判断是否存在连续相同的两个字符,连续两个字符一模一样
  • 原文地址:https://blog.csdn.net/weixin_43791477/article/details/125438343