-
Notifications
You must be signed in to change notification settings - Fork 0
Solve 252_meeting_rooms_easy #12
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
| - `start`, `end` の変数名を `start_time`, `end_time` に変更して、より明確にした。 | ||
|
|
||
| # 別解・模範解答 | ||
| `intervals` からすべての時間帯のペアを取り出し、そのペアが重なるかどうかを確認する方法。 |
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.
他に、(開始時間, 1)、(終了時間, -1) と書かれた紙を箱に入れて、小さい順に取り出して、開始と終了が交互に出てくるかどうかを確認するという方法もありそうですね。
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.
レビューありがとうございます。
確かにそうですね、早速実装してみました。
TIME_POINT_START = 1
TIME_POINT_END = -1
class Solution:
def canAttendMeetings(self, intervals: list[list[int]]) -> bool:
time_points: list[tuple[int, int]] = []
for start_time, end_time in intervals:
time_points.append((start_time, TIME_POINT_START))
time_points.append((end_time, TIME_POINT_END))
time_points.sort()
# time_points must have start time and end time in alternating order after sorting
# if not, then there are overlapping intervals
previous_time_type = TIME_POINT_END
for _, time_type in time_points:
if time_type == previous_time_type:
return False
previous_time_type = time_type
return True| start_time2: int, | ||
| end_time2: int, | ||
| ) -> bool: | ||
| if start_time1 < end_time1 <= start_time2 < end_time2: |
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.
return max(start_time1, start_time2) < min(end_time1, end_time2)と書けると思いました。ただ、ややパズルに感じられるため、元のコードのほうがよいかもしれません。
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 Rooms - LeetCode
言語
Python
問題の概要
ある人が複数の会議に出席できるかを判定する問題である。
[starti, endi]で表される。intervalsで与えられる。例1:
例2:
制約:
[starti, endi](開始 < 終了、かつ0以上1,000,000以下)自分の解法
intervalsの時間帯を開始時間でソートし、隣接する会議の終了時間と開始時間を比較する方法。ひとつでも重なっている会議があれば、Falseを返す。O(nlogn)O(1)step2
start,endの変数名をstart_time,end_timeに変更して、より明確にした。別解・模範解答
intervalsからすべての時間帯のペアを取り出し、そのペアが重なるかどうかを確認する方法。O(n^2)(すべてのペアを確認するため)O(1)次に解く問題の予告