Open
Conversation
naoto-iwase
reviewed
Jan 3, 2026
Comment on lines
+18
to
+27
| 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.
確かにこのように書けますが、可読性・保守性いずれの観点からも個人的には避けたいと感じました。
以下のようなナイーブな書き方で十分かなと感じます。
remaining_intervals = [
(interval.start, interval.end) for interval in intervals
]
heapq.heapify(remaining_intervals)
Comment on lines
+139
to
+141
| 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.
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.
一重ループで書くとこうなります。
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.
上のコードをGemini 3 Proにレビューしてもらったら、
Pythonの sort() (Timsort) は非常に高速に最適化されています。全ての要素を出し入れする場合は、heapq を使うよりも、最初に sort() して for ループやポインタで走査する方がシンプルで高速な場合が多いです。
とのことでした。これはそうかもしれません。
時間計算量はheapifyはO(n)ですが、ループ内でのheappopがn回行われるため全体O(n log n)で、実質ソートと同じコストがかかり、定数倍の勝負です。これは綿密にチューニングされたsort()に平均的に勝てる気がしないですね。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題文(代用):https://neetcode.io/problems/meeting-schedule-ii/question