Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/meeting-rooms-ii/description/

問題文(プレミアムの場合)
Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2

Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1

Constraints:
1 <= intervals.length <= 104
0 <= starti < endi <= 106

備考

次に解く問題の予告
Is Subsequence

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、accumulate.cpp、priority_queue.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

Comment on lines +17 to +18
RoomCounter result = accumulate(time_with_labels.begin(), time_with_labels.end(),
RoomCounter{0, 0}, updateRoomCounter);
Copy link

Choose a reason for hiding this comment

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

まあ、いいですが、for で開いて書いたほうが読みやすいし、後々書き換えやすいだろうと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
Pythonのfoldを使った解法があったので調べたところC++ではaccumulateが似た機能を持つとございました。
step1~step3のようにforで回す方が自分も好みでした。

Comment on lines 11 to 12
time_with_labels.emplace_back(interval[0], "start");
time_with_labels.emplace_back(interval[1], "end");
Copy link

Choose a reason for hiding this comment

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

end, start がこの辞書順であることを利用していることに気がつけというのは、結構パズルを作っていると思います。

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.ido8j91sj503

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
数字を使うなど代替案を考えましたがコメントを追加することにしました。
他の方の解法を読んでから実装したものなので前知識がございました。

確かに初見でこれを読んだ時は解読する必要がありますね。パズルになっていないかは意識します。
4ad1595

int min_required_rooms = 0;
int current_used_rooms = 0;
// 時間ごとの鍵の貸し出し数のうち最大のものを探す
for (const auto& [time, num_used_rooms] : time_to_room_reservation) {
Copy link

Choose a reason for hiding this comment

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

time は C++ では time.h の time を思い浮かべますね。後使っていないので、警告が出ることがあるが、structured bindings では難しいなどの話があります。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
確かに調べたところtime.hの中にtimeは既にありました。
https://learn.microsoft.com/ja-jp/cpp/c-runtime-library/reference/time-time32-time64?view=msvc-170

ありそうな言葉は調べる癖をつけないとですね。occupied_timeとしてみました。

int min_required_rooms = 0;
int current_using_rooms = 0;
// 時間ごとの鍵の貸し出し数のうち最大のものを探す
for (const auto& [using_time, num_key] : num_keys) {
Copy link

Choose a reason for hiding this comment

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

using_time は言語感覚に合わないですが、しかしベターなアイディアがあるわけではありません。

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.

3 participants