Open
Conversation
tokuhirat
reviewed
Sep 11, 2025
| else: # START | ||
| num_rooms += 1 | ||
| max_num_rooms = max(max_num_rooms, num_rooms) | ||
| return max_num_rooms |
There was a problem hiding this comment.
START, END と対応して num_rooms を +1, -1 しているので、STARTを開始の印とするだけではなくて部屋の変化数 +1、ENDを部屋の変化数 -1 とする方が値に必然性がでて良いかなと個人的には思います。
https://github.com/tokuhirat/LeetCode/pull/56/files#r2292380034
tokuhirat
reviewed
Sep 11, 2025
| def minMeetingRooms(self, intervals: list[list[int]]) -> int: | ||
| if not intervals: | ||
| return 0 | ||
| intervals.sort() |
tokuhirat
reviewed
Sep 11, 2025
| if not intervals: | ||
| return 0 | ||
| intervals.sort() | ||
| start_time, end_time = intervals[0] |
There was a problem hiding this comment.
start_time は使っていないので _, end_time = intervals[0] とするのもありかと思いました。
tokuhirat
reviewed
Sep 11, 2025
| return 0 | ||
| intervals.sort() | ||
| start_time, end_time = intervals[0] | ||
| heap = [] |
There was a problem hiding this comment.
上で書かれているように meeting_end_times の方がわかりやすいと思いました。また、直後で heappush しているので heap であることがわかり、step1 よりも読みやすかったです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題へのリンク
253. Meeting Rooms II
言語
Python
問題の概要
会議室のスケジュールを管理するために、最小限の会議室を確保するとき、いくつの会議室が必要かを求める問題。各会議は開始時刻と終了時刻で表され、すべての会議が重複しないように会議室を割り当てる必要がある。
自分の解法
step1
O(n log n)O(n)step2
heappushとheappopを使用する代わりにheapreplaceを使用することで、ヒープの操作を効率化&コードが明快に。heappopはヒープの最小要素を削除したあと、ヒープの最後の要素を根に移動し、ヒープ木を再構築するheappushも新しい要素を追加したあと、ヒープ木を再構築するheapreplaceは最小要素を削除して新しい要素を追加する操作を1回で行うため、効率的である。step3
step3_1.py:一度思いついてしまえばヒープよりも簡単に実装できるstep3_2.py:ヒープを用いた解法step4 (FB)
別解・模範解答
Timeline Sweep
O(n log n)O(n)Two Pointers
O(n log n)O(n)次に解く問題の予告