Skip to content

Commit

Permalink
Create meeting-rooms-ii.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Aug 8, 2015
1 parent bbaa441 commit cc946a0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions C++/meeting-rooms-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time: O(nlogn)
// Space: O(n)

/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
vector<int> starts, ends;
for (const auto& i : intervals) {
starts.emplace_back(i.start);
ends.emplace_back(i.end);
}

sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());

int min_rooms = 0, cnt_rooms = 0;
int s = 0, e = 0;
while (s < starts.size()) {
if (starts[s] < ends[e]) {
++cnt_rooms; // Acquire a room.
// Update the min number of rooms.
min_rooms = max(min_rooms, cnt_rooms);
++s;
} else {
--cnt_rooms; // Release a room.
++e;
}
}
return min_rooms;
}
};

0 comments on commit cc946a0

Please sign in to comment.