[leetcode] 729: My Calendar I

Description

Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.

Your class will have the 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 double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)

For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.

Your class will be called like this: MyCalendar cal = new MyCalendar(); ``MyCalendar.book(start, end)

Example 1

MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
**Explanation**: 
The first event can be booked.  The second can not because time 15 is already booked by another event.
The third event can be booked, as the first event takes every time less than 20, but not including 20.

Note

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

Solution

简单来说就是向MyCalendar类中插入区间[start, end),如果插入的区间和MyCalendar中已保存的区间有重叠就返回false,否则返回true。可以借鉴二叉搜索树的思想来解决这个问题,将一个区间作为二叉搜索树的节点,插入的区间如果和当前区间有重叠,则返回false,如果插入的区间在当前区间的右侧,则往右子树寻找插入点,反之往左寻找插入点,插入成功返回true,具体代码如下所示。

class Node{
    /**
     * 该节点代表的区间
     */
    public int start;
    public int end;
    /**
     * 左右子树
     */
    public Node left;
    public Node right;
    public Node(int start, int end) {
        this.start = start;
        this.end = end;
    }
}

public class MyCalendarOne {

    private Node root = null;

    public MyCalendarOne() {
    }

    public boolean book(int start, int end){
        // 第一个节点
        if (null == root){
            root = new Node(start, end);
            return true;
        }

        // 从根节点往下寻找插入点
        Node insertNode = new Node(start, end);
        Node preNode = null;
        Node currNode = root;
        boolean insertLeft = true;  //标记是插入左子树还是右子树中
        while (null != currNode){
            preNode = currNode;
            if (insertNode.end <= currNode.start){
                currNode = currNode.left;
                insertLeft = true;
            }else if (insertNode.start >= currNode.end){
                currNode = currNode.right;
                insertLeft = false;
            }else {
                return false;
            }
        }

        //找到了插入点
        if (insertLeft){
            preNode.left = new Node(start, end);
        }else {
            preNode.right = new Node(start, end);
        }
        return true;
    }
}

Complexity Analysis

  • 事件复杂度: 假设当前为第 n n 次插入,二叉搜索树的平均高度为log(n),最大高度为 n n n为当前已经保存下来的最大事件个数,因此平均查找插入节点的时间复杂度为 O(log(n)) O ( l o g ( n ) ) ,插入 n n 次的平均复杂度为O(nlog(n)),最大时间复杂度为 O(n2) O ( n 2 )
  • 空间复杂度: 插入 n n 次最多用了n个节点来保存结果,因此空间复杂度为 O(n) O ( n )

另外leetcode还推荐了一种更加简洁的solution,但因为每次都需要遍历所有已经插入的节点,因此时间复杂度为 O(n2) O ( n 2 )

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值