Skip to content

Create 253. Meeting Rooms II.md#56

Open
tokuhirat wants to merge 1 commit intomainfrom
253.-Meeting-Rooms-II
Open

Create 253. Meeting Rooms II.md#56
tokuhirat wants to merge 1 commit intomainfrom
253.-Meeting-Rooms-II

Conversation

@tokuhirat
Copy link
Owner

This Problem
253. Meeting Rooms II
Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2

Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1

Constraints:
0 <= intervals.length <= 10^4
intervals[i].length == 2
0 <= starti < endi <= 10^6

Next Ploblem
392. Is Subsequence
言語: Python

Copy link

@Fuminiton Fuminiton left a comment

Choose a reason for hiding this comment

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

いいと思います


class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
times = sum(intervals, [])
Copy link

@Fuminiton Fuminiton Aug 20, 2025

Choose a reason for hiding this comment

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

osモジュールにtimesがあるので別の名前が良いかと思います。

os.times()
現在の全体的なプロセス時間を返します。返り値は 5 個の属性を持つオブジェクトになります:

https://docs.python.org/ja/3.13/library/os.html#os.times

Copy link
Owner Author

Choose a reason for hiding this comment

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

その観点は見落としていました。ありがとうございます。

Copy link

@Kaichi-Irie Kaichi-Irie Sep 12, 2025

Choose a reason for hiding this comment

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

自分も知りませんでした。ありがとうございます。

min_num_rooms = 0
num_occupied_rooms = 0
for _, delta in events:
num_occupied_rooms += delta

Choose a reason for hiding this comment

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

好みだと思いますが、
eventがstartであることと、eventがstartだから1を足す話は別な気がするので、分けた方が親切な気がしました。
具体的には、以下のようなイメージです。

        MEETING_START = 1
        MEETING_END = -1

        events = []
        for start_time, end_time in intervals:
            events.append((start_time, MEETING_START))
            events.append((end_time, MEETING_END))
        
        num_occupied = 0
        num_needed = 0
        events.sort()
        for event_time, event_type in events:
            if event_type == MEETING_END:
                num_occupied -= 1
                continue
            num_occupied += 1
            num_needed = max(num_needed, num_occupied)

Copy link
Owner Author

Choose a reason for hiding this comment

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

個人的には違う感覚だなと思いました。
というのもMEETING_STARTとMEETING_ENDの大小関係がソートに影響してアルゴリズムが正しく動くかを左右します。単に部屋数の増減を直接書けば1と-1は意味を持ちますが(コメントで補足すべきと思います)、MEETING_STARTの値は意味を持っていない(値に必然性がない)ためです。

Choose a reason for hiding this comment

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

回答ありがとうございます。

自分はeventsを(時間, 会議が始まる or 終わる)と捉えていたのですが、tokuhirat さんは初めから(時間, 必要な部屋数が1つ増えるかor1つ減るか)で考えていたということですね。

納得しました。よくよく考えると、tokuhirat さんのやり方の方がシンプルで良いと思いました。ある会議で部屋数を複数個使うみたいな状況にも対応できますし。

# so a room is freed before being reused.
events.sort()

min_num_rooms = 0

Choose a reason for hiding this comment

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

optional: required_rooms などもありですかね。

Comment on lines +95 to +96
events.append((start, 1)) # one more room needed
events.append((end, -1)) # one room freed

Choose a reason for hiding this comment

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

ROOM_REQUIRED, ROOM_RELEASED のような定数に置くのも一案かと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

個人的には定数にするなら絶対値は同じなので一つかなという気がします。コメントありがとうございます。

NUM_ROOMS_PER_MEETING = 1

events.append((start, +NUM_ROOMS_PER_MEETING))  # secure room(s)
events.append((end, -NUM_ROOMS_PER_MEETING))  # free room(s)

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.

4 participants