Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions 253/253.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## 何も見ずに解いてみる

```cpp
class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
multimap<int, int> time_to_increment;
for (const vector<int>& interval : intervals) {
time_to_increment.insert({interval[0], +1});
time_to_increment.insert({interval[1], -1});
}
int max_num_rooms = 0;
int num_rooms = 0;
for (const auto& [_, increment] : time_to_increment) {
num_rooms += increment;
max_num_rooms = max(max_num_rooms, num_rooms);
}
return max_num_rooms;
}
};
```

## 他の人のコードを見てみる

https://github.com/tokuhirat/LeetCode/pull/56/files
https://github.com/olsen-blue/Arai60/pull/57/files
https://github.com/ryosuketc/leetcode_arai60/pull/56/files
https://github.com/Ryotaro25/leetcode_first60/pull/61/files

cppのmapやunordered_mapはpythonでいうdefaultdictみたいな振る舞いをするらしい。([]で存在しない要素にアクセスしようとすると自動でvalueがコンストラクタから初期化される)

Choose a reason for hiding this comment

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

レファレンスはこの辺りでしょうか。
https://en.cppreference.com/w/cpp/container/map/operator_at.html

Returns a reference to the value that is mapped to a key equivalent to key or x respectively, performing an insertion if such key does not already exist.


## 最終コード

mapでいちいちやるよりvectorに全部入れてからsortしたほうがちょっと早いような気もするけどまあこれで良い気がします。

```cpp
class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
map<int, int> time_to_num_change;
for (const vector<int>& interval : intervals) {
++time_to_num_change[interval[0]];
--time_to_num_change[interval[1]];
}
int max_num_rooms = 0;

Choose a reason for hiding this comment

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

好みですが、min_required_rooms などの方が素直に読めそうだなとは思います (ただ関数の返り値であることは明らかなので、これで十分とは思いますが)。

int num_rooms = 0;
for (const pair<int, int>& [_, num_change] : time_to_num_change) {

Choose a reason for hiding this comment

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

上で書かれているように auto を使っても良いかなと思いました。num_change が int だろうと下の行でわかりそうで、使わない _ の型を書くのは微妙かなという理由です。

Copy link
Owner Author

@potrue potrue Aug 24, 2025

Choose a reason for hiding this comment

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

これは確かにそうですね。読むにあたってノイズとなる情報かもしれません。

num_rooms += num_change;
max_num_rooms = max(max_num_rooms, num_rooms);
}
return max_num_rooms;

Choose a reason for hiding this comment

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

問題文の表現に合わせて min を使った方が自然かなと思いました。

}
};
```