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 };
  • 相关阅读:
    一道压强题
    考试习惯的审题+习题+电脑存放目录记录
    产品需求分类及KANO模型需求排序学习
    马斯洛需求层次理论及其新拓展学习笔记
    12-JQuery学习之bind绑定事件
    11-JQuery学习之ready预加载事件
    09-JQuery学习之删除元素
    10-JQuery学习之遍历元素
    08-JQuery学习之创建元素和添加元素
    06-JQuery学习之操作元素的样式
  • 原文地址:https://www.cnblogs.com/easonliu/p/4785020.html
Copyright © 2011-2022 走看看