Skip to content

Conversation

@Satorien
Copy link
Owner

@Satorien Satorien commented 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)

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)

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実はend_timesの方もheapで十分ですね。

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_days

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_days

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()に平均的に勝てる気がしないですね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants