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:

  1. The problem is very easy!!!
  2. But I have made a mistake in defining comparator.
  3. Comparator<Interval> interval_sort = new Comparator<Interval>() {
  4. @Override
  5. public int compare(Interval interval_1, Interval interval_2) {
  6. if (interval_1.start < interval_2.start)
  7. return interval_1.start - interval_2.start;
  8. return interval_1.end - interval_2.end;
  9. }
  10. };
  11.  
  12. Error case:
  13. Runtime Error Message:
  14. Line 53: java.lang.IllegalArgumentException: Comparison method violates its general contract!
  15.  
  16. This is a logic error for the above code.
  17. suppose we have interval_1 and interval_2.
  18. iff interval_1.start < interval_2.start, we would return
  19. return interval_1.start - interval_2.start; <a negative number>
  20.  
  21. The error part is at otherwise.
  22. What if we swap the reference of them, can we still get the same answer?
  23. Iff "interval_1.start > interval_2.start" ?
  24. Then we use the end for the comparision!!!! Why don't just use start!!!! Wrong ! Right!
  25.  
  26. If you indeed want take the end into consideration. You should use "==", which would not break the comparision's logic.
  27. if (interval_1.start == interval_2.start)
  28. return return interval_1.end - interval_2.end;
  29. return interval_1.start - interval_2.start;

Solution:

  1. public class Solution {
  2. public boolean canAttendMeetings(Interval[] intervals) {
  3. if (intervals == null)
  4. throw new IllegalArgumentException("intervals is null");
  5. int len = intervals.length;
  6. if (len <= 1)
  7. return true;
  8. Comparator<Interval> interval_sort = new Comparator<Interval>() {
  9. @Override
  10. public int compare(Interval interval_1, Interval interval_2) {
  11. return interval_1.start - interval_2.start;
  12. }
  13. };
  14. Arrays.sort(intervals, interval_sort);
  15. for (int i = 1; i < len; i++) {
  16. if (intervals[i-1].end > intervals[i].start)
  17. return false;
  18. }
  19. return true;
  20. }
  21. }

[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. RMQ问题与ST算法

    RMQ(Range Minimum/Maximum Query)问题是求区间最值问题. 对于长度为 n 的数组 A,进行若干次查询,对于区间 [L,R] 返回数组A中下标在 [L,R] 中的最小(大) ...

  2. 移动设备日期选择插件(基于JQUERY)

    上周花了2个小时写的一个日期选择插件,比较适合移动端的设备.先看个效果图吧.如果刚好是你需要的就往下吧,不需要的也可以继续..... 其实网络上已经有的了类似的成熟插件,比如基于mobiscroll, ...

  3. (转)JS获取当前对象大小以及屏幕分辨率等

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  4. virtualbox共享文件夹无访问权限问题解决方法

    virtualbox共享文件夹无访问权限问题解决方法 早就困扰了,这次新装虚拟机又碰到了,记录下来. 这篇文章主要介绍了virtualbox共享文件夹无访问权限问题解决方法,造成这个问题的原因是不跟v ...

  5. iOS 正则表达式-判断邮箱、手机号

    判断是否是邮箱 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[ ...

  6. VS2008中MFC界面编程Caption中文全是乱码的解决办法 -转载

    一.问题 在预览状态下可能看到中文,但是编译运行后对话框中的中文全是问号.即使你用的VS中文版,即使你也用了Unicode编码,即使有条件编译 #ifdef _WIN32LANGUAGE LANG_C ...

  7. 【elasticsearch】(2)centos7 超简单安装elasticsearch 的监控、测试的集群工具elasticsearch head

    elasticsearch-head是elasticsearch(下面称ES)比较普遍使用的可监控.测试等功能的集群管理工具,是由H5编写的单独的网页程序.使用方法网上很多,这里教大家一个超简单安装h ...

  8. VBA开发经验总结之一:利用Range对象设计用户界面

    读罢<EXCEL专业开发>,最大的震撼就是著者对VBA技术的追求以及对Excel艺术品般的设计.受到此书著者的启发,也打算把自己在日常开发中一些经验总结出来,一来作为自己的知识储备,二来也 ...

  9. winform程序开机自动启动代码

    几天前头儿要我实现程序能开机自动启动,搞好了,整理起来写下来. private void checkBox1_CheckedChanged(object sender, EventArgs e) { ...

  10. runas /user:administrator cmd 以管理员身份运行CMD

    runas /user:administrator cmd 以管理员身份运行CMD 1.windows+r打开 2.然后根据提示输入密码