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. 在用VS2010连接oracle数据库时ORA-12504错误

    在用VS2010连接oracle数据库时,可能会出现: ORA-12504: TNS: 监听程序在 CONNECT_DATA 中未获得 SERVICE_NAME 只需在web.config文件Data ...

  2. 曾经感动过我们的文字 今天是否还有印象?——v1

    ①人最宝贵的东西是生命.生命对人来说只有一次.因此,人的一生应当这样度过:当一个人回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧;这样,在他临死的时候,能够说,我把整个生命和全部精力都献给了人生 ...

  3. MES项目中出现的一个事务嵌套的使用场景

    昨天在MES项目中,需要在业务逻辑的几个关键点记录错误信息,需要把错误信息写入数据表. 但是由于整个业务逻辑都是包在一个事务模板里面的 比如这样的: WhhTransactionTemplate tr ...

  4. Css 梯形图形 并添加文字

    HTML页面的代码: <body> <div style="width:500px;border:solid 1px #ccc;"> <div> ...

  5. IOS-objectForKey与valueForKey在NSDictionary中的差异

    从 NSDictionary 取值的时候有两个方法,objectForKey: 和 valueForKey:,这两个方法具体有什么不同呢? 先从 NSDictionary 文档中来看这两个方法的定义: ...

  6. CSS-id选择器-类选择器-属性选择器

    Css基础 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 选择器通常是您需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成. 每个属性有一个值.属性和值被冒号分开. 下 ...

  7. angularJS广播

    控制器之间共享数据(向父级/子级控制器传递event,data),类似于service在不同的控制器中通信 html: <div ng-controller="ParentCtrl&q ...

  8. Android学习3—电话拨号器

    本测试主要实现了一个Android的拨打电话的功能 一:界面预览 由图中可以看出,这个Activity需要3个控件:TextView.EditText.Button 其实实现一个功能要经过几个步骤: ...

  9. phpMyAdmin下载与安装

    Part1 phpMyAdmin下载 浏览器输入网址 http://www.phpmyadmin.net 下载即可 我的下载是这样的 Part2 phpMyAdmin安装 解压下载的压缩包到apach ...

  10. SurfaceFlinger服务概述和学习计划

    SurfaceFlinger服务负责绘制Android应用程序的UI 实现相当复杂,要从正面分析它的实现不是一件容易的事.既然不能从正面分析,我们就想办法从侧面分析.说到底,无论SurfaceFlin ...