LeetCode 729 My Calendar I

Problem 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.

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)

题目描述:实现一个MyCalendar类,来存放事件。每当有新的事件添加时候,检测是否会导致冲突。

我想到的这个题的数据结构与LeetCode56 Merge Intervals 一样,就是一个list<interval>,按照start的顺序排列。要速度快,则需要实现二分查找。这个二分查找写的时候费了一些力气。

我二分查找的思路是:查找将来插入新的interval的位置,范围是从0到n(list的长度为n,index为0到n-1),当当前的位置mid的end <= 带插入的start,lo往右移动。因为之前没有冲突,则此时lo的index所指的位置就将是新插入的event的位置,可能是需要将新的event插入到此位置,也可能是将lo所指的位置和新的event连在一起。

class MyCalendar {
    class interval {
        int start;
        int end;
        public interval() {}
        public interval(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }
    List<interval> list;
    public MyCalendar() {
        list = new ArrayList<>();
    }
    
    public boolean book(int start, int end) {
        // for(int i = 0; i < list.size(); i++) {
        //     System.out.print("[" + list.get(i).start + "," + list.get(i).end + "], ");
        // }
        // System.out.println();
        if(end <= start) return true;
        //binary searcy
        if(list.size() == 0) {
            list.add(new interval(start, end));
            return true;
        }
        int lo = 0, hi = list.size();
        
        while(lo < hi) {
            int mid = (lo + hi) / 2;
            if(list.get(mid).end <= start) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        // lo.end >= start
        //System.out.println(lo);
        if(lo == list.size() || list.get(lo).start >= end) {
            if(lo != list.size() && list.get(lo).start == end) {
                list.get(lo).start = start;
            } else {
                list.add(lo, new interval(start, end));
            }
            return true;
        }
        return false;
    }
}

这种写法较快,但是比较难想。

在discuss看到有shawn gao大神用TreeMap的解法,感觉很方便巧妙。用start作key,end作val。

class MyCalendar {

    TreeMap<Integer, Integer> calendar;

    public MyCalendar() {
        calendar = new TreeMap<>();
    }

    public boolean book(int start, int end) {
        Integer floorKey = calendar.floorKey(start);
        if (floorKey != null && calendar.get(floorKey) > start) return false;
        Integer ceilingKey = calendar.ceilingKey(start);
        if (ceilingKey != null && ceilingKey < end) return false;

        calendar.put(start, end);
        return true;
    }
}

 

转载于:https://www.cnblogs.com/rookielet/p/10539963.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值