-
Notifications
You must be signed in to change notification settings - Fork 0
253 meeting rooms ii medium #21
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?
Conversation
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
START, END と対応して num_rooms を +1, -1 しているので、STARTを開始の印とするだけではなくて部屋の変化数 +1、ENDを部屋の変化数 -1 とする方が値に必然性がでて良いかなと個人的には思います。
https://github.com/tokuhirat/LeetCode/pull/56/files#r2292380034
| def minMeetingRooms(self, intervals: list[list[int]]) -> int: | ||
| if not intervals: | ||
| return 0 | ||
| intervals.sort() |
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.
入力を変更している点が気になりました。
| if not intervals: | ||
| return 0 | ||
| intervals.sort() | ||
| start_time, end_time = intervals[0] |
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.
start_time は使っていないので _, end_time = intervals[0] とするのもありかと思いました。
| return 0 | ||
| intervals.sort() | ||
| start_time, end_time = intervals[0] | ||
| heap = [] |
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.
上で書かれているように meeting_end_times の方がわかりやすいと思いました。また、直後で heappush しているので heap であることがわかり、step1 よりも読みやすかったです。
問題へのリンク
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)次に解く問題の予告