-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 253. Meeting Rooms II #49
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
Open
Fuminiton
wants to merge
1
commit into
main
Choose a base branch
from
solve-problem49
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| ## 取り組み方 | ||
| - step1: 5分以内に空で書いてAcceptedされるまで解く + テストケースと関連する知識を連想してみる | ||
| - step2: 他の方の記録を読んで連想すべき知識や実装を把握した上で、前提を置いた状態で最適な手法を選択し実装する | ||
| - step3: 10分以内に1回もエラーを出さずに3回連続で解く | ||
|
|
||
| ## step1 | ||
| 開始地点に+1、終了地点に-1を加えると、0~制限時間最大までを1刻みで累積していけば、intervalの期間だけ+1された結果が得られるので、intervalsの全ての要素に対して適用する。 | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def minMeetingRooms(self, intervals: List[List[int]]) -> int: | ||
| TIME_LIMITS = 1000000 | ||
| num_bookings = [0] * (TIME_LIMITS + 1) | ||
| for start, end in intervals: | ||
| num_bookings[start] += 1 | ||
| num_bookings[end] -= 1 | ||
|
|
||
| max_num_bookings = 0 | ||
| for i in range(TIME_LIMITS): | ||
| num_bookings[i + 1] += num_bookings[i] | ||
| max_num_bookings = max(max_num_bookings, num_bookings[i + 1]) | ||
| return max_num_bookings | ||
| ``` | ||
|
|
||
| ## step2 | ||
| ### 読んだ | ||
| https://github.com/olsen-blue/Arai60/pull/57/files | ||
| https://github.com/hayashi-ay/leetcode/pull/62/files | ||
| https://github.com/Yoshiki-Iwasa/Arai60/pull/61/files | ||
|
|
||
| ### 感想 | ||
| - step1の方法は10^6より細かく時間を分けたい場合どうしようかと考えていた | ||
| - が、start or endのイベントタイプとその時刻をタプルとして管理すれば、時刻でソートした結果を見ていくだけで効率的に同時刻のミーティング数を数え上がられる | ||
| - step1ではケアできているが、同時刻に開始と終了がある場合の数え上げの順序は気をつけたい | ||
| - startとendの時刻をそれぞれ入れる配列を用意して、ポインターの進め方を工夫するやり方もよさそう | ||
| - 最小の値から時刻を見ていきたいので、ソートではなくヒープを使っている方もいたが、どちらでもよさそう | ||
|
|
||
| ##### event_time, event_type のタプルで管理する | ||
| ```py | ||
| class Solution: | ||
| def minMeetingRooms(self, intervals: List[List[int]]) -> int: | ||
| START = 1 | ||
| END = -1 | ||
| events = [] | ||
| for start_time, end_time in intervals: | ||
| events.append((start_time, START)) | ||
| events.append((end_time, END)) | ||
|
|
||
| num_needed_rooms = 0 | ||
| num_ongoing = 0 | ||
| events.sort() | ||
| for event_time, event_type in events: | ||
| if event_type == END: | ||
| num_ongoing -= 1 | ||
| continue | ||
| num_ongoing += 1 | ||
| num_needed_rooms = max(num_needed_rooms, num_ongoing) | ||
|
|
||
| return num_needed_rooms | ||
| ``` | ||
|
|
||
| ##### タプルを使わず、startとend用に別々の配列を用意する | ||
| ```py | ||
| class Solution: | ||
| def minMeetingRooms(self, intervals: List[List[int]]) -> int: | ||
| if not intervals: | ||
| return 0 | ||
|
|
||
| start_times = [] | ||
| end_times = [] | ||
| for start_time, end_time in intervals: | ||
| start_times.append(start_time) | ||
| end_times.append(end_time) | ||
| start_times.sort() | ||
| end_times.sort() | ||
|
|
||
| num_needed_rooms = 0 | ||
| num_ongoing = 0 | ||
| start_times_pointer = 0 | ||
| end_times_pointer = 0 | ||
| while start_times_pointer < len(start_times): | ||
| if start_times[start_times_pointer] >= end_times[end_times_pointer]: | ||
| num_ongoing -= 1 | ||
| end_times_pointer += 1 | ||
| continue | ||
| num_ongoing += 1 | ||
| num_needed_rooms = max(num_needed_rooms, num_ongoing) | ||
| start_times_pointer += 1 | ||
|
|
||
| return num_needed_rooms | ||
| ``` | ||
|
|
||
| ## step3 | ||
| num_occupiedにすれば、neededもoccupiedもroomの話をしているのかと分かるからよりよいと思った | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def minMeetingRooms(self, intervals: List[List[int]]) -> int: | ||
| MEETING_START = 1 | ||
| MEETING_END = -1 | ||
|
|
||
| events = [] | ||
| for start_time, end_time in intervals: | ||
| events.append((start_time, MEETING_START)) | ||
| events.append((end_time, MEETING_END)) | ||
|
|
||
| num_occupied = 0 | ||
| num_needed = 0 | ||
| events.sort() | ||
| for event_time, event_type in events: | ||
| if event_type == MEETING_END: | ||
| num_occupied -= 1 | ||
| continue | ||
| num_occupied += 1 | ||
| num_needed = max(num_needed, num_occupied) | ||
|
|
||
| return num_needed | ||
| ``` | ||
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.
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.
で、
intervals = [(0, 1000000)] * 10000の場合、計算ステップが 1010 となり、 TLE するように思います。 LeetCode にはそのような厳しいテストケースが入力されていなかったため、たまたま TLE しなかったのだと思います。コードを書く前に時間計算量を求め、時間計算量からおおよその計算ステップ数を求め、計算ステップからおおよその処理時間を求め、許容される処理時間以内に処理が終わるかどうかを確認する癖を付けられることをお勧めいたします。
計算ステップ数から処理時間を求める方法については、過去の他の方のコードレビューのコメントをご参照ください。
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.
レビューありがとうございます。
こちら考えはしたのですが、記述が漏れておりました。申し訳ありません。
今回最も時間を要するのが、ミーティングの時間を記録しつつ必要な部屋の数を計算する19-21行目の処理で、O(10^6)となり、1秒程度で終わる処理だと見込んでおりました。
手元の環境で以下を5回実行してみたところ、0.36秒程度で終わることを確認できました。
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.
コードを読み間違えていました。失礼いたしました。 19~21 行目のコードは range(TIME_LIMITS) 回呼ばれるのですね。それであれば、おっしゃる通りだと思います。