Conversation
| if (intervals.first.end > intervals.second.start && | ||
| intervals.second.end > intervals.first.start) { |
There was a problem hiding this comment.
ここは数直線上(時系列順)になっていた方が分かりやすいと思いました。
| 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) { |
There was a problem hiding this comment.
ここも個人的には時系列順に書きたくなりますが、「i の end が i+1 の start よりも後」というのも十分伝わるので、趣味の範囲だと思います。
| if (sorted[i].end > sorted[i + 1].start) { | |
| if (sorted[i + 1].start < sorted[i].end) { |
| return true; | ||
| } | ||
|
|
||
| std::vector<Interval> sorted = intervals; |
There was a problem hiding this comment.
sorted.cpp では受け取った invervals を並び替えてしまっていますが、ここのようにコピーして使うのが良いと思いました。
実際はそもそも、引数を const にして守ってあげるのが良さそうですね。
| std::vector<Interval> sorted = intervals; | ||
|
|
||
| std::sort(sorted.begin(), sorted.end(), | ||
| [](const Interval& a, const Interval& b) { | ||
| return a.start < b.start; | ||
| } | ||
| ); |
There was a problem hiding this comment.
細かいですが、inverals をソートしたものを手に入れる、という文脈で、ここは行を空けないほうが自然に思いました。
| 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; |
There was a problem hiding this comment.
C++に詳しくないのでソートしなくていいのかな、と思ったのですが、std::mapはキー昇順で並び替えてくれるんですね(便利!)。
https://en.cppreference.com/w/cpp/container/map.html
There was a problem hiding this comment.
で、キーをソートしなくても構わない構造として、unordered_mapがあるんですね(そういえば、こちらはチラホラ見かけていました)。
https://en.cppreference.com/w/cpp/container/unordered_map.html
| 会議室の鍵か。思いつかなかった。`keys.cpp` に書いた。 | ||
| > 鍵を借りる時間と返す時間をばらばらにソートして、鍵の数が2つ以上でたかを確認という手もありますね。 |
|
全体的に読みやすかったです 👍 |
| Interval, | ||
| std::vector<Interval>, | ||
| decltype(comparison) | ||
| > pq(comparison); |
There was a problem hiding this comment.
型名もしくは型名から文字を削って省略形にした文字列を変数名に入れても、読み手にとってあまり情報が増えないように思います。ただ、あまり良い変数名が思いつきませんでした。無理やりつけるなら、 ordered_by_start なのですが、いまいちしっくりきませんでした。
問題
https://neetcode.io/problems/meeting-schedule/question
次の問題
Meeting Rooms 2
代替問題