zoukankan      html  css  js  c++  java
  • [LeetCode] Meeting Rooms I & II

    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.

     1 /**
     2  * Definition for an interval.
     3  * struct Interval {
     4  *     int start;
     5  *     int end;
     6  *     Interval() : start(0), end(0) {}
     7  *     Interval(int s, int e) : start(s), end(e) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool canAttendMeetings(vector<Interval>& intervals) {
    13         sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b) {
    14             return a.start < b.start;
    15         });
    16         for (int i = 1; i < intervals.size(); ++i) {
    17             if (intervals[i].start < intervals[i-1].end) return false;
    18         }
    19         return true;
    20     }
    21 };

    Meeting Rooms II

    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.

    For example,
    Given [[0, 30],[5, 10],[15, 20]],
    return 2.

     1 /**
     2  * Definition for an interval.
     3  * struct Interval {
     4  *     int start;
     5  *     int end;
     6  *     Interval() : start(0), end(0) {}
     7  *     Interval(int s, int e) : start(s), end(e) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     int minMeetingRooms(vector<Interval>& intervals) {
    13         vector<pair<int, int>> schedule;
    14         for (auto interval : intervals) {
    15             schedule.push_back({interval.start, 1});
    16             schedule.push_back({interval.end, -1});
    17         }
    18         sort(schedule.begin(), schedule.end());
    19         int cnt = 0, res = 0;
    20         for (auto s : schedule) {
    21             if (s.second == 1) ++cnt;
    22             else --cnt;
    23             res = max(res, cnt);
    24         }
    25         return res;
    26     }
    27 };
  • 相关阅读:
    hihocoder #1138 : Islands Travel
    关于c中的inline
    LUOGU P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
    LUOGU P1908 逆序对
    归并排序 (模板)
    tyvj 1864 守卫者的挑战
    loj #10001. 「一本通 1.1 例 2」种树
    bzoj 1026: [SCOI2009]windy数
    BZOJ 4521: [Cqoi2016]手机号码
    LUOGU 3089 后缀排序(模板)
  • 原文地址:https://www.cnblogs.com/easonliu/p/4785020.html
Copyright © 2011-2022 走看看