Leetcode-252.Meeting Rooms

Problem Description:

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.


Analysis:
The idea is : we sort the time intervals, then check the overlapping of each pair of neighbouring intervals.

The definition of Compare function :
Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Function object:

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

Ps: Compare function must be a static function which belongs to class, it can’t be a member function in a class.

Code:

bool canAttendMeetings(vector<Interval>& intervals) 
 {
    sort(intervals.begin(), intervals.end(), compare);
    int n = intervals.size();
    for (int i = 0; i < n - 1; ++i)
        if (intervals[i].end > intervals[i + 1].start)
            return false;
        return true;
 }
static bool compare(Interval & inter1, Interval & inter2)
{
    return inter1.start < inter2.start;
}

The sort time complexity is O(nlogn) and the check overlapping take O(n). so the overall time complexity is O(nlogn).


Follow up:
If we want to find the minimum number of conference rooms required.


Idea:

Create event for each start and end of intervals. Then for start event, open one more room; for end event, close one meeting room. At the same time, update the most rooms that is required.

Code:

int minMeetingRooms(vector& intervals) 
 {
    map<int, int> mp;
    for (int i = 0; i < intervals.size(); ++i)
    {
        mp[intervals[i].start]++;
        mp[intervals[i].end]--;
    }
    int cnt = 0, maxCnt = 0;
    for (auto i : mp)
    {
        cnt += i.second;
        maxCnt = max(maxCnt, cnt);
    }
    return maxCnt;
 }

Using priority_queue to store the end time of the each meeting room, pop out the room if the the meeting is over, if the smallest meeting isn’t over, then we have to add a new room, as well push the Interval to the pq, and count++, update the result.

int minMeetingRooms(vector<Interval>& intervals) {
    sort(intervals.begin(), intervals.end(),[](Interval & a, Interval &b) {return a.start <= b.start;});
    priority_queue<int, vector<int> , greater<int> > end_pq;
    int count = 0, result = 0;
    for (Interval elem : intervals) {
        while (!end_pq.empty() && end_pq.top() <= elem.start) {
            end_pq.pop();
            --count;
        }
        end_pq.push(elem.end);
        ++count;
        result = max(count, result);
    }
    return result;
}

Another way is : Group those non-overlapping meetings in the same room and then count how many rooms we need.

int findNonoverlapping(vector<Interval>& rooms, Interval & interval)
 {
    for (int i = 0; i < rooms.size(); ++i)
        if (interval.start >= rooms[i].end)
            return i;
    return -1;
 }

int minMeetingRooms(vector<Interval>& intervals) 
 {
    sort (intervals.begin(), intervals.end(),[](Interval & s1, Interval &s2){return s1.start < s2.start;});
    vector<Interval> rooms; //save the room[i] last meeting
    for (auto interval : intervals)
    {
        int idx = findNonoverlapping(rooms, interval);
        if (rooms.empty() || idx == -1)
            rooms.push_back(interval);
        else rooms[idx] = interval;
    }
    return rooms.size();
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值