-
Notifications
You must be signed in to change notification settings - Fork 0
252. Meeting Rooms #25
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,73 @@ | ||
| # 252. Meeting Rooms | ||
| - 問題: https://leetcode.com/problems/meeting-rooms | ||
| - Premium問題のためNeetCodeを利用した: https://neetcode.io/problems/meeting-schedule | ||
| - 言語: Python | ||
|
|
||
| ## Step1 | ||
| - 前の会議終了時刻と次の会議開始時刻を比較して、前の会議終了時刻 > 次の会議開始時刻であればFalseを返す | ||
| - `intervals` はソートされていないので、安定ソートで会議開始時刻でソートする | ||
|
|
||
| ### 解答(AC) | ||
| ```py | ||
| class Solution: | ||
| def canAttendMeetings(self, intervals: List[Interval]) -> bool: | ||
| sorted_intervals = sorted(intervals, key=lambda interval: interval.start) | ||
| previous_interval_end = -1 | ||
|
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. (指名されてないですがすみません) |
||
| for interval in sorted_intervals: | ||
| if previous_interval_end > interval.start: | ||
| return False | ||
| previous_interval_end = interval.end | ||
|
|
||
| return True | ||
| ``` | ||
| - 解答時間: 10:22 | ||
| - `for interval in sorted_intervals:` は分割代入した方が読みやすかったかも | ||
|
|
||
| ## Step2 | ||
| - 典型コメント集: なし | ||
| - https://github.com/potrue/leetcode/pull/55 | ||
| - C++ | ||
| - `(開始時刻, True)` 、 `(終了時刻, False)` の組でリストを作成 | ||
| - 2番目の真偽値は、会議に出席しているかどうかの状態を表す | ||
| - 上のリストを時刻でソート | ||
| - 変数名 `attending` は意味論として分かりやすいと思った | ||
| - https://github.com/tokuhirat/LeetCode/pull/55 | ||
| - Python | ||
| - > 終了時刻が早い順に見ていき、それより早く開始する会議があれば参加できない | ||
| - 終了時刻でソートしているところ以外は同じ | ||
| - 参加可能な会議数を求めるなどの汎用性が出る | ||
| - https://github.com/olsen-blue/Arai60/pull/56 | ||
| - Python | ||
| - 累積和の問題として解釈 | ||
|
|
||
| ### 読みやすく直したコード | ||
| ```py | ||
| class Solution: | ||
| def canAttendMeetings(self, intervals: List[Interval]) -> bool: | ||
| sorted_intervals = sorted(intervals, key=lambda x: x.end) | ||
| last_meeting_end = -1 | ||
| for interval in sorted_intervals: | ||
|
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. 好みですが、直接 |
||
| if interval.start < last_meeting_end: | ||
| return False | ||
| last_meeting_end = interval.end | ||
|
|
||
| return True | ||
| ``` | ||
|
|
||
| ## Step3 | ||
| ```py | ||
| class Solution: | ||
| def canAttendMeetings(self, intervals: List[Interval]) -> bool: | ||
| sorted_intervals = sorted(intervals, key=lambda x: x.end) | ||
|
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. 個人的にはinterval.startを見ていくのでx.startでsortした方が分かりやすい気がしました。 |
||
| last_meeting_end = -1 | ||
| for interval in sorted_intervals: | ||
| if interval.start < last_meeting_end: | ||
| return False | ||
| last_meeting_end = interval.end | ||
|
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. last_meetingそれ自体を変数に格納してもいいかもしれないと思いました。 みたいな感じです。質的に同じものを比較しているということがわかりやすくなる気がします。 |
||
|
|
||
| return True | ||
| ``` | ||
| - 解答時間: | ||
| - 1回目: 2:00 | ||
| - 2回目: 1:47 | ||
| - 3回目: 1:38 | ||
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.
本筋ではないですが、問題固有のクラス (Interval) がある場合、一回は定義部分を記載いただけるとレビュワーとしては助かると思いました。