[LeetCode#252] Meeting Rooms
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的更多相关文章
- [LeetCode] 252. Meeting Rooms 会议室
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- LeetCode 252. Meeting Rooms (会议室)$
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [leetcode]252. Meeting Rooms会议室有冲突吗
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 253. Meeting Rooms II 会议室 II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 253. Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- 252. Meeting Rooms 区间会议室
[抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...
- 【LeetCode】252. Meeting Rooms 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- 252. Meeting Rooms
题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
- [LeetCode#253] Meeting Rooms II
Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...
随机推荐
- Customize the SharePoint 2013 search experience with a Content Enrichment web service
Did you ever wish you had more control over how your content is indexed and presented as search resu ...
- php程序员的开始
最近又懒惰了,博客没有更新,学习一直在停止,反思自己最近在学习什么了,但是脑子里面空白的一片,让我冒汗了.程序是一个不断的积累,最近在学习的路上,发现自己懂的越来越少,人就有点急躁了,什么都想学,导致 ...
- 标准web架构分层
标准Web系统的架构分层 转载:http://blog.csdn.net/yinwenjie http://blog.csdn.net/yinwenjie/article/details/464 ...
- Css3 兼容新旧浏览器
想想10年前用 IE6,火狐,遨游,谷歌等浏览器学习css时,那叫一个艰苦,各种hack各种抓耳挠腮,不是margin塌陷就是元素飞了... 当前借着css3这个东风,如果各大浏览器厂商能统一一下,也 ...
- (转)META http-equiv="refresh" 实现网页自动跳转
***.html自动跳转文件代码如下: <HTML> <HEAD><META http-equiv="Refresh" content="5 ...
- RelativeLayout相对布局 安卓布局技巧
http://blog.csdn.net/nieweiking/article/details/38417317 RelativeLayout相对布局 相对布局 RelativeLayout 允许子元 ...
- c语言学习之基础知识点介绍(十):数组
本节主要介绍数组. 一.数组 /* 数组:一个变量可以存n个变量. 语法:类型 数组名[长度(正整数)]; 例如:int score[5];//定义了一个int类型的数组,长度为5,可以保存5个数据. ...
- RABBITMQ安装注意点
关于 RABBITMQ的配置问题安装问题windows7 和window 10我都试了windows10安装和配置不要出现中文和空格,不然你日寒飞的心都有了ERLANG的安装也是Win7直接默认的路径 ...
- A题笔记(8)
No. 2878 No. 2559 都是输入两个数,让你来判断是否符合要求的 特别注意 2878 , 题目中要求 1<=a,b<=2^64-1(2的64次方-1)= 18446744073 ...
- Delphi 类方法和普通方法的区别 .
//类声明 TMyClass = class public class procedure MyProc; //类方式 constructor Create; //Crea ...