zoukankan      html  css  js  c++  java
  • Leetcode Meeting Rooms

    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.


    解题思路:

    先对start sort, O(nlgn), 然后比较Si+1 >= Ei, if true, continue compare, if false, return false. Total Complexity : O(nlgn)

    问题: 注意Java 里的Arrays.sort(intervals, new Comparator<Interval>(){...} ); 使用方法


    Java code:

    /**
     * Definition for an interval.
     * public class Interval {
     *     int start;
     *     int end;
     *     Interval() { start = 0; end = 0; }
     *     Interval(int s, int e) { start = s; end = e; }
     * }
     */
    public class Solution {
        public boolean canAttendMeetings(Interval[] intervals) {
            if(intervals.length <= 1) {
                return true;
            }
            Arrays.sort(intervals, new Comparator<Interval>(){
                public int compare(Interval a, Interval b) {
                    return a.start - b.start;
                }
            });
            for(int i = 1; i < intervals.length; i++) {
                if(intervals[i].start < intervals[i-1].end) {
                    return false;
                }
            }
            return true;
        }
    }

    Reference:

    1. https://leetcode.com/discuss/50912/ac-clean-java-solution

  • 相关阅读:
    多播委托和匿名方法再加上Lambda表达式
    委托
    从警察抓小偷看委托
    StringBuilder
    C#修饰符详解
    数据结构与算法之队列
    数据结构与算法之栈
    win10重复安装
    网络编程基础
    PrintPreviewControl
  • 原文地址:https://www.cnblogs.com/anne-vista/p/4856639.html
Copyright © 2011-2022 走看看