资讯专栏INFORMATION COLUMN

[LintCode/LeetCode] Meeting Rooms

Noodles / 1889人阅读

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.

Example

Given intervals = [[0,30],[5,10],[15,20]], return false.

Note

两次循环遍历

Solution
public class Solution {
    /**
     * @param intervals: an array of meeting time intervals
     * @return: if a person could attend all meetings
     */
    public boolean canAttendMeetings(List intervals) {
        // Write your code here
        int size = intervals.size();
        for (int i = 0; i < size-1; i++) {
            Interval a = intervals.get(i);
            for (int j = i+1; j < size; j++) {
                Interval b = intervals.get(j);
                if ((a.start > b.start && a.start < b.end) || 
                    (b.start > a.start && b.start < a.end)) {
                    return false;
                }
            }
        }
        return true;
    }
}
Update 2018-10
class Solution {
    public boolean canAttendMeetings(Interval[] intervals) {
        if (intervals == null || intervals.length < 2) return true;
        List list = Arrays.asList(intervals);
        Collections.sort(list, (a, b)->a.start-b.start);
        Interval pre = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i).start < pre.end) return false;
            else pre = list.get(i);
        }
        return true;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71341.html

相关文章

  • [LintCode/LeetCode] Best Meeting Point

    Problem A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance ...

    morgan 评论0 收藏0
  • Meeting Rooms & Meeting Rooms II

    摘要:思路这道题就是要找区间之间是否有。而的复杂度是,所以最后总的复杂度为。思路的条件依然是不同的是这题需要求房间数。还是先,指向之前有的最小的那一个。接着的是,比小,所以又放入。。的是,比大,因此出,放入。。 Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[...

    TalkingData 评论0 收藏0
  • [LeetCode] 253. Meeting Rooms II

    Problem Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. Example 1: Input: [[0, 30],[5,...

    mengera88 评论0 收藏0
  • [Leetcode] Meeting Rooms 会议室

    摘要:排序法复杂度时间空间思路这题和很像,我们按开始时间把这些都给排序后,就挨个检查是否有冲突就行了。有冲突的定义是开始时间小于之前最晚的结束时间。这里之前最晚的结束时间不一定是上一个的结束时间,所以我们更新的时候要取最大值。 Meeting Rooms Given an array of meeting time intervals consisting of start and end...

    jubincn 评论0 收藏0
  • [LintCode/LeetCode] Word Break

    Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...

    dunizb 评论0 收藏0

发表评论

0条评论

Noodles

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<