731. 我的日程安排表 II

106 阅读1分钟

题目:
实现一个 MyCalendar 类来存放你的日程安排。如果要添加的时间内不会导致三重预订时,则可以存储这个新的日程安排。

MyCalendar 有一个 book(int start, int end)方法。它意味着在 startend 时间内增加一个日程安排,注意,这里的时间是半开区间,即 [start, end), 实数 x 的范围为,  start <= x < end

当三个日程安排有一些时间上的交叉时(例如三个日程安排都在同一时间内),就会产生三重预订。

每次调用 MyCalendar.book方法时,如果可以将日程安排成功添加到日历中而不会导致三重预订,返回 true。否则,返回 false 并且不要将该日程安排添加到日历中。

请按照以下步骤调用MyCalendar 类: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)

算法:
方法一:线段树
注意start, end的意义是查询或者更新范围,其实在递归调用时不用变化,只需要调整node。 先进行查询范围内的重叠预定次数,如果当前范围内最大预定重叠次数已经为2,那么在范围内再增加一次预定,则必然产生三次重叠预定。

type MyCalendarTwo struct {
    Left, Right int
    Lazy, Value int // Value保存 区间的最大值
    LeftChild, RightChild *MyCalendarTwo
}


func Constructor() MyCalendarTwo {
    return MyCalendarTwo{
        Left: 0,
        Right: 1e9,
    }
}


func (this *MyCalendarTwo) Book(start int, end int) bool {
    a := query(this, start, end - 1) 
    if a >= 2 {
        return false
    }
    add(this, start, end - 1, 1)
    return true
}


func query(node *MyCalendarTwo, start, end int) int {

    if start <= node.Left && node.Right <= end {
        return node.Value
    }
    lazeCreate(node)
    pushDown(node)
    mid := node.Left + (node.Right - node.Left) / 2
    if end <= mid {
        return query(node.LeftChild, start, end)
    } else if mid < start {
        return query(node.RightChild, start, end)
    }
    return max(query(node.LeftChild, start, mid), query(node.RightChild, mid + 1, end))
}

func add(node *MyCalendarTwo, start, end, value int) {
    if end < node.Left || node.Right < start {
        return 
    }
    if start <= node.Left && node.Right <= end {
        node.Value = node.Value + value 
        node.Lazy = node.Lazy + value 
        return
    }  
    lazeCreate(node)
    pushDown(node)
    // mid := node.Left + (node.Right - node.Left) / 2
    add(node.LeftChild, start, end, value)
    add(node.RightChild, start, end, value)
    pushUp(node)
}

func pushDown(node *MyCalendarTwo) {
    if node.Lazy == 0 {
        return
    }
    node.LeftChild.Value = node.LeftChild.Value + node.Lazy
    node.RightChild.Value = node.RightChild.Value + node.Lazy
    node.LeftChild.Lazy = node.LeftChild.Lazy + node.Lazy
    node.RightChild.Lazy = node.RightChild.Lazy + node.Lazy
    node.Lazy = 0
}

func pushUp(node *MyCalendarTwo) {
    node.Value = max(node.LeftChild.Value, node.RightChild.Value)
}

func newNode(left, right int) *MyCalendarTwo {
    // fmt.Println(left, right)
    return &MyCalendarTwo{
        Left: left,
        Right: right,
    }
}
func lazeCreate(node *MyCalendarTwo) {
    mid := node.Left + (node.Right - node.Left) / 2
    if node.LeftChild == nil {
        node.LeftChild = newNode(node.Left, mid)
    }
    if node.RightChild == nil {
        node.RightChild = newNode(mid + 1, node.Right)
    }
}

func max(a, b int) int {
    if a > b {
        return a 
    }
    return b
}
 /**
 * Your MyCalendarTwo object will be instantiated and called as such:
 * obj := Constructor();
 * param_1 := obj.Book(start,end);
 */