Problem 252

Posted by Ruizhi Ma on November 24, 2020

Solution URL

https://leetcode.com/submissions/detail/423823026/

代码

class Solution {
    public boolean canAttendMeetings(int[][] intervals) {
        //time: O(N)
        //space: O(1)
        //sort intervals according to the ascending order of every first element
        Arrays.sort(intervals, new Comparator<int[]>() {
            public int compare(int[] a, int[] b){
                return a[0] - b[0];
            }
        });

        //traverse the array, compare second element of the former one
        //and first element of the last one. If second element is larger, return false
        for(int i = 0; i < intervals.length - 1; i++){
            if(intervals[i][1] > intervals[i + 1][0]) return false;
        }

        return true;
    }
}