-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/253. Meeting Rooms II #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?
Conversation
| def interval_lt(self, other) -> bool: | ||
| if self.start < other.start: | ||
| return True | ||
| return False | ||
|
|
||
| Interval.__lt__ = interval_lt | ||
|
|
||
| class Solution: | ||
| def minMeetingRooms(self, intervals: List[Interval]) -> int: | ||
| heapq.heapify(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.
確かにこのように書けますが、可読性・保守性いずれの観点からも個人的には避けたいと感じました。
以下のようなナイーブな書き方で十分かなと感じます。
remaining_intervals = [
(interval.start, interval.end) for interval in intervals
]
heapq.heapify(remaining_intervals)| while end_times and end_times[-1] <= interval.start: | ||
| end_times.pop() | ||
| bisect.insort(end_times, interval.end, key=lambda x: -x) |
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.
実はend_timesの方も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.
import heapq
class Solution:
def minMeetingRooms(self, intervals: List[Interval]) -> int:
remaining_intervals = [
(interval.start, interval.end) for interval in intervals
]
heapq.heapify(remaining_intervals)
holding_end_times = []
required_days = 0
while remaining_intervals:
next_start, next_end = heapq.heappop(remaining_intervals)
while holding_end_times and next_start >= holding_end_times[0]:
heapq.heappop(holding_end_times)
heapq.heappush(holding_end_times, next_end)
required_days = max(required_days, len(holding_end_times))
return required_daysThere 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.
一重ループで書くとこうなります。
import heapq
class Solution:
def minMeetingRooms(self, intervals: list[Interval]) -> int:
start_times = [interval.start for interval in intervals]
end_times = [interval.end for interval in intervals]
heapq.heapify(start_times)
heapq.heapify(end_times)
required_days = 0
max_required_days = 0
while start_times and end_times:
if start_times[0] < end_times[0]:
heapq.heappop(start_times)
required_days += 1
max_required_days = max(max_required_days, required_days)
else:
heapq.heappop(end_times)
required_days -= 1
return max_required_daysThere 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.
上のコードをGemini 3 Proにレビューしてもらったら、
Pythonの sort() (Timsort) は非常に高速に最適化されています。全ての要素を出し入れする場合は、heapq を使うよりも、最初に sort() して for ループやポインタで走査する方がシンプルで高速な場合が多いです。
とのことでした。これはそうかもしれません。
時間計算量はheapifyはO(n)ですが、ループ内でのheappopがn回行われるため全体O(n log n)で、実質ソートと同じコストがかかり、定数倍の勝負です。これは綿密にチューニングされたsort()に平均的に勝てる気がしないですね。
問題文(代用):https://neetcode.io/problems/meeting-schedule-ii/question