2402 meeting rooms 3#45
Open
kitano-kazuki wants to merge 3 commits into
Open
Conversation
nodchip
reviewed
May 8, 2026
|
|
||
| #### 思ったこと | ||
|
|
||
| * 遅延予約の対応 |
There was a problem hiding this comment.
自分も解いてみました。自分も遅延された予約がある場合の処理が複雑になりそうだと思いました。結局、遅延された予約の処理をしない方向で考え、ミーティングルームが空いていなかったら、空くまで待ってからミーティングを開始するという方向に落ち着きました。どちらが良いかは好みの範囲だと思います。
class Solution {
public:
int mostBooked(int n, vector<vector<int>>& meetings) {
std::sort(meetings.begin(), meetings.end());
std::vector<int> num_meetings(n);
std::set<int> available_meeting_rooms;
for (int i = 0; i < n; ++i) {
available_meeting_rooms.insert(i);
}
// 終了時刻, 部屋番号
std::priority_queue<std::pair<int64_t, int>, std::vector<std::pair<int64_t, int>>, std::greater<std::pair<int64_t, int>>> ongoing_meetings;
for (const vector<int>& meeting : meetings) {
int64_t start = meeting[0];
int64_t end = meeting[1];
// 終了したミーティングを取り除く。
while (!ongoing_meetings.empty() && ongoing_meetings.top().first <= start) {
int room = ongoing_meetings.top().second;
ongoing_meetings.pop();
available_meeting_rooms.insert(room);
}
// 部屋が空いていたらミーティングを開始する。
if (!available_meeting_rooms.empty()) {
int room = *available_meeting_rooms.begin();
available_meeting_rooms.erase(available_meeting_rooms.begin());
++num_meetings[room];
ongoing_meetings.push(std::make_pair(end, room));
continue;
}
// 部屋が空いていなかったら、空くまで待ってミーティングを開始する。
int64_t last_end = ongoing_meetings.top().first;
int room = ongoing_meetings.top().second;
ongoing_meetings.pop();
int64_t new_end = end - start + max(last_end, start);
++num_meetings[room];
ongoing_meetings.push(std::make_pair(new_end, room));
}
int max_meetings = *std::max_element(num_meetings.begin(), num_meetings.end());
return std::distance(num_meetings.begin(), std::find(num_meetings.begin(), num_meetings.end(), max_meetings));
}
};
Owner
Author
There was a problem hiding this comment.
解いていただきありがとうございます。
空くまで待ってからミーティングを開始するという方向
これは思いつかなかったです。
直近で解放される時刻に今処理中の予約の所要時間を足して終了予定時刻として, ongoing_meetingsに追加するということですね。
予約の確保を遅延しないので, 処理を追うのが楽で自分もいいと思いました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/meeting-rooms-iii/description