-
Notifications
You must be signed in to change notification settings - Fork 0
Create 252. Meeting Rooms.md #55
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # 252. Meeting Rooms | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - どこかでみたことがある。終了時刻が早い順に見ていき、それより早く開始する会議があれば参加できない。 | ||
| ```python | ||
| class Solution: | ||
| def canAttendMeetings(self, intervals: List[List[int]]) -> bool: | ||
| sorted_intervals = sorted(intervals, key=lambda x: x[1]) | ||
| last_end = 0 | ||
| for start, end in sorted_intervals: | ||
| if start < last_end: | ||
| return False | ||
| last_end = end | ||
| return True | ||
| ``` | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://github.com/olsen-blue/Arai60/pull/56/files | ||
| - 各時刻について重複がないか累積和の考え方を使うコード。思いつかなかった。開始と終了を見れば十分なので自然かと言われると微妙なところだと思った。 | ||
| - 座標圧縮をすると入力が整数でない場合も対応できる。効率化も可能。 | ||
| - https://github.com/Mike0121/LeetCode/pull/27/files | ||
| - sort する際に終了時刻を基準にするか、開始時刻を基準にするかはどちらでも良いと思う。 | ||
| - https://github.com/hayashi-ay/leetcode/pull/59/files | ||
| - Heap も活用できる。途中で会議が追加される場合に優位性あり。 | ||
| - 下記コードは in-place で intervals をヒープにしている。 | ||
| https://docs.python.org/ja/3.13/library/heapq.html#heapq.heapify | ||
| ```python | ||
| import heapq | ||
|
|
||
|
|
||
| class Solution: | ||
| def canAttendMeetings(self, intervals: List[List[int]]) -> bool: | ||
| heapq.heapify(intervals) | ||
| last_end = 0 | ||
| while intervals: | ||
| start, end = heapq.heappop(intervals) | ||
| if start < last_end: | ||
| return False | ||
| last_end = end | ||
| return True | ||
| ``` | ||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| ```python | ||
| class Solution: | ||
| def canAttendMeetings(self, intervals: List[List[int]]) -> bool: | ||
| intervals = sorted(intervals) | ||
| last_end = 0 | ||
| for start, end in intervals: | ||
| if last_end > start: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 時間の流れを意識しやすそうなので、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 趣味の範囲ですが、私は条件式は、 一方で、グリッド探索中の範囲内チェックでは、
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます。あまり意識せずに書いていました。 |
||
| return False | ||
| last_end = end | ||
| return True | ||
| ``` | ||
|
|
||
| 1分,1分,1分で3回Accept | ||
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.
この方法であれば出席可能な会議数の最大数を求められるので、汎用性があって良いかもです。
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.
なるほど、そこの汎用性は気が付いていませんでした。以下のような感じでしょうか。
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.
はい。良いと思います。
するとこれも解けると思います。
https://leetcode.com/problems/non-overlapping-intervals/description/