Problem:

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return false.

Analysis:

The problem is very easy!!!
But I have made a mistake in defining comparator.
Comparator<Interval> interval_sort = new Comparator<Interval>() {
@Override
public int compare(Interval interval_1, Interval interval_2) {
if (interval_1.start < interval_2.start)
return interval_1.start - interval_2.start;
return interval_1.end - interval_2.end;
}
}; Error case:
Runtime Error Message:
Line 53: java.lang.IllegalArgumentException: Comparison method violates its general contract! This is a logic error for the above code.
suppose we have interval_1 and interval_2.
iff interval_1.start < interval_2.start, we would return
return interval_1.start - interval_2.start; <a negative number> The error part is at otherwise.
What if we swap the reference of them, can we still get the same answer?
Iff "interval_1.start > interval_2.start" ?
Then we use the end for the comparision!!!! Why don't just use start!!!! Wrong ! Right! If you indeed want take the end into consideration. You should use "==", which would not break the comparision's logic.
if (interval_1.start == interval_2.start)
return return interval_1.end - interval_2.end;
return interval_1.start - interval_2.start;

Solution:

public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if (intervals == null)
throw new IllegalArgumentException("intervals is null");
int len = intervals.length;
if (len <= 1)
return true;
Comparator<Interval> interval_sort = new Comparator<Interval>() {
@Override
public int compare(Interval interval_1, Interval interval_2) {
return interval_1.start - interval_2.start;
}
};
Arrays.sort(intervals, interval_sort);
for (int i = 1; i < len; i++) {
if (intervals[i-1].end > intervals[i].start)
return false;
}
return true;
}
}

[LeetCode#252] Meeting Rooms的更多相关文章

  1. [LeetCode] 252. Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  2. LeetCode 252. Meeting Rooms (会议室)$

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  3. [leetcode]252. Meeting Rooms会议室有冲突吗

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  4. [LeetCode] 253. Meeting Rooms II 会议室 II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. [LeetCode] 253. Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  6. 252. Meeting Rooms 区间会议室

    [抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...

  7. 【LeetCode】252. Meeting Rooms 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...

  8. 252. Meeting Rooms

    题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...

  9. [LeetCode#253] Meeting Rooms II

    Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...

随机推荐

  1. linux下的openoffice安装和服务自启动

    openoffice下载并安装 wget http://sourceforge.net/projects/openofficeorg.mirror/files/4.1.1/binaries/zh-CN ...

  2. Parallel类(简化Task 操作)

    Parallel类 Parallel类是对线程的一个很好抽象.该类位于System.Threading.Tasks命名空间中,提供了数据和任务并行性. 1.用Parallel.For()方法循环 // ...

  3. ionic 项目分享No.2——简化版【转】

    写在文章前:由于最近研究ionic框架,深感这块的Demo寥寥可数,而大家又都藏私,堂堂天朝,何时才有百家争鸣之象,开源精神吾辈当仁不让!                                ...

  4. Jquery 限制文本框输入字数【转】

    <script type="text/javascript" src="js/jquery.min.js" ></script> < ...

  5. Android Studio SDK Manager无法正常下载如何设置

    博客分类: Linux 零散小知识 Android那点事 AndroidStudioSDKManager  一方面在/etc/hosts中设置: #Google主页 203.208.46.146 ww ...

  6. mvc5 + ef6 + autofac搭建项目(四)

    在列表页面,点击新增,弹出窗口实现视屏上传,这里存在一个问题,就是大文件上传的问题,iis出于安全问题,有限制,当然这不是大问题,解决也很容易: 见截图: 请忽略视屏文件,看得懂的请装作不懂. 源码 ...

  7. Spring处理id相同的bean

    http://www.360doc.com/content/13/1018/05/41237_322247510.shtml(应该可以解决) http://www.2cto.com/kf/201601 ...

  8. oracle编译 失效对象方式

    如果procedure 所使用的表结构发生了改变等其它情况,在相应的xxx_objects表的status字段会变为invalid状态,但是如果在调用时procedure会自动编译,grant失效对象 ...

  9. 修改tomcat默认的端口号

    协同管理系统黙认使用Tomcat默认的端口8080,除8080端口外Tomcat还会占用8005,8009和8443端口.如果这4个端口已被占用,可以将协同管理系统修改为使用其它端口. 修改方法如下: ...

  10. iOS10新增Api详解

    1.SiriKit SiriKit的功能非常强大,支持音频.视频.消息发送接收.搜索照片.预订行程.管理锻炼等等.在用到此服务时,siri会发送Intent对象,里面包括用户的请求和各种数据,可以对这 ...