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. apache的一些基本配置

    Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 主站点的配置(基本配置) 基本配置: ServerRoot "/mnt/softw ...

  2. Plain old data structure(POD)

    Plain old data structure, 缩写为POD, 是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中.POD通常被用在系统的边界处,即指不同系统之间只 ...

  3. 转-C# 操作 Excel 常见问题收集和整理

    经常会有项目需要把表格导出为 Excel 文件,或者是导入一份 Excel 来操作,那么如何在 C# 中操作 Excel 文件成了一个最基本的问题. 做开发这几年来,陆陆续续也接触过这样的需求,但因为 ...

  4. PHPStorm&PHPstudy环境配置

    因为实习要求,最近在学php,补下开发环境的配置,原博客链接:点击打开链接 1.创建新的项目(project),创建完成之后单击工具栏的应用运行/调试(Select Run/Debug Configu ...

  5. 关于word-break,word-wrap换行

    目前项目中有一些流程日志需要动态显示到页面上,实现方法是ajax动态获取附加到<span></span>标签上,然后设置word-break:break-all样式使其自动换行 ...

  6. asp.net命名规范

    以下命名规范是在编程中,可以辅助快速编程的良好方式之一,我一点点的整理出来,以便形成自己的编程规范.还有待完善... 0.产品命名规范: 结构 层次 产品 模块 功能 命名规则 UI(界面层) Web ...

  7. js--Ajax的小知识(二):处理ajax的session过期的请求

    问题的产生: 现如今Ajax在Web项目中应用广泛,几乎可以说无处不在. 有时会碰到这样个问题:当Ajax请求遇到Session超时,应该怎么办? 显而易见,传统的页面跳转在此已经不适用,因为Ajax ...

  8. ListPreference之entries和entryValues

    在使用PreferenceActivity时,碰到配置文件的ListPreference有两个属性android:entries,android:entryValues.这两个属性其实就和html的o ...

  9. jfreechart环形图完美实现

    邮件发送由于不支持js,项目只能在后台生成环形图,用jfreechart完全可以实现,即:RingPlot. 这就拿jfreechart生成的最终效果,依赖jar包jfreechart,如果有任何细节 ...

  10. Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)

    一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o te ...