Skip to content

252. Meeting Rooms#55

Open
dxxsxsxkx wants to merge 1 commit into283_move_zeroesfrom
252_meeting_rooms
Open

252. Meeting Rooms#55
dxxsxsxkx wants to merge 1 commit into283_move_zeroesfrom
252_meeting_rooms

Conversation

@dxxsxsxkx
Copy link
Copy Markdown
Owner

Comment on lines +30 to +31
if (intervals.first.end > intervals.second.start &&
intervals.second.end > intervals.first.start) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは数直線上(時系列順)になっていた方が分かりやすいと思いました。

Suggested change
if (intervals.first.end > intervals.second.start &&
intervals.second.end > intervals.first.start) {
if (intervals.second.start < intervals.first.end &&
intervals.first.start < intervals.second.end) {

);

for (int i = 0; i < sorted.size() - 1; ++i) {
if (sorted[i].end > sorted[i + 1].start) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここも個人的には時系列順に書きたくなりますが、「iendi+1start よりも後」というのも十分伝わるので、趣味の範囲だと思います。

Suggested change
if (sorted[i].end > sorted[i + 1].start) {
if (sorted[i + 1].start < sorted[i].end) {

return true;
}

std::vector<Interval> sorted = intervals;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorted.cpp では受け取った invervals を並び替えてしまっていますが、ここのようにコピーして使うのが良いと思いました。
実際はそもそも、引数を const にして守ってあげるのが良さそうですね。

Comment on lines +22 to +28
std::vector<Interval> sorted = intervals;

std::sort(sorted.begin(), sorted.end(),
[](const Interval& a, const Interval& b) {
return a.start < b.start;
}
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かいですが、inverals をソートしたものを手に入れる、という文脈で、ここは行を空けないほうが自然に思いました。

Suggested change
std::vector<Interval> sorted = intervals;
std::sort(sorted.begin(), sorted.end(),
[](const Interval& a, const Interval& b) {
return a.start < b.start;
}
);
std::vector<Interval> sorted = intervals;
std::sort(sorted.begin(), sorted.end(),
[](const Interval& a, const Interval& b) {
return a.start < b.start;
}
);

class Solution {
public:
bool canAttendMeetings(vector<Interval>& intervals) {
std::map<int, int> num_keys_needed;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++に詳しくないのでソートしなくていいのかな、と思ったのですが、std::mapはキー昇順で並び替えてくれるんですね(便利!)。
https://en.cppreference.com/w/cpp/container/map.html

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

で、キーをソートしなくても構わない構造として、unordered_mapがあるんですね(そういえば、こちらはチラホラ見かけていました)。
https://en.cppreference.com/w/cpp/container/unordered_map.html

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapは平衡二分探索木で実装されているんですよね。

Comment on lines +36 to +37
会議室の鍵か。思いつかなかった。`keys.cpp` に書いた。
> 鍵を借りる時間と返す時間をばらばらにソートして、鍵の数が2つ以上でたかを確認という手もありますね。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

room という抽象化もできますね。私はこちらの方がしっくりきました。

@mamo3gr
Copy link
Copy Markdown

mamo3gr commented Mar 27, 2026

全体的に読みやすかったです 👍

Interval,
std::vector<Interval>,
decltype(comparison)
> pq(comparison);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

型名もしくは型名から文字を削って省略形にした文字列を変数名に入れても、読み手にとってあまり情報が増えないように思います。ただ、あまり良い変数名が思いつきませんでした。無理やりつけるなら、 ordered_by_start なのですが、いまいちしっくりきませんでした。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants