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]

代码

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

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]

代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值