package com.freetymekiyan.algorithms.level.easy;

import java.util.Arrays;

/**
 * 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.
 * <p>
 * For example,
 * Given [[0, 30],[5, 10],[15, 20]],
 * return false.
 * <p>
 * Company Tags: Facebook
 * Tags: Sort
 * Similar Problems: (H) Merge Intervals, (M) Meeting Rooms II
 */
public class MeetingRooms {

    /**
     * Sort.
     * The actual problem is detecting whether there is any overlap between meetings.
     * Sort the intervals.
     * From second interval, check each interval with the previous one.
     * If current interval's start < previous interval's end, return false.
     */
    public boolean canAttendMeetings(Interval[] intervals) {
        Arrays.sort(intervals, (i1, i2) -> i1.start - i2.start);
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i].start < intervals[i - 1].end) {
                return false;
            }
        }
        return true;
    }

    public class Interval {

        int start;
        int end;

        Interval() {
            start = 0;
            end = 0;
        }

        Interval(int s, int e) {
            start = s;
            end = e;
        }
    }

}

@@ -54,30 +54,147 @@
 ort 
-java.util.Arrays;%0A%0A/**
+com.freetymekiyan.algorithms.utils.Utils.Interval;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Comparator;%0A%0A/**%0A * 251. Meeting Rooms%0A * %3Cp%3E
 %0A * 
@@ -959,40 +959,46 @@
 ls, 
-(i1, i2) -%3E i1.start
+Comparator.comparingInt(i
  -
+%3E
  i
-2
 .start)
+)
 ;%0A  
@@ -1194,243 +1194,5 @@
   %7D%0A
-%0A    public class Interval %7B%0A%0A        int start;%0A        int end;%0A%0A        Interval() %7B%0A            start = 0;%0A            end = 0;%0A        %7D%0A%0A        Interval(int s, int e) %7B%0A            start = s;%0A            end = e;%0A        %7D%0A    %7D%0A%0A%7D%0A
+%7D

update 251. Meeting Rooms