Leetcode Meeting Rooms

Source: Internet
Author: User

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 .

Problem Solving Ideas:

First to start sort, O (NLGN), then compare si+1 >= Ei, if true, continue compare, if False, return false. Total Complexity:o (NLGN)

Issue: Note the arrays.sort in Java (intervals, new comparator<interval> () {...}), how to use

Java Code:

/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { Public Booleancanattendmeetings (interval[] intervals) {if(Intervals.length <= 1) {            return true; } arrays.sort (Intervals,NewComparator<interval>(){             Public intCompare (Interval A, Interval b) {returnA.start-B.start;        }        });  for(inti = 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

Leetcode Meeting Rooms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.