-
Notifications
You must be signed in to change notification settings - Fork 0
253. Meeting Rooms II #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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がコンストラクタから初期化される) | ||
|
|
||
| ## 最終コード | ||
|
|
||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好みですが、 |
||
| int num_rooms = 0; | ||
| for (const pair<int, int>& [_, num_change] : time_to_num_change) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上で書かれているように auto を使っても良いかなと思いました。num_change が int だろうと下の行でわかりそうで、使わない _ の型を書くのは微妙かなという理由です。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 問題文の表現に合わせて min を使った方が自然かなと思いました。 |
||
| } | ||
| }; | ||
| ``` | ||
There was a problem hiding this comment.
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