Skip to content
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

253. Meeting Rooms II #62

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

253. Meeting Rooms II #62

wants to merge 7 commits into from

Conversation

hayashi-ay
Copy link
Owner

https://leetcode.com/problems/meeting-rooms-ii/

Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
# intervalは左閉右開区間なのでsortした際にENDがSTARTより先に来て欲しい
MTG_START = 1
Copy link

Choose a reason for hiding this comment

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

チーム内で合意形成が得られている場合を除き、変数名の単語はフルスペルで書くことをお勧めいたします。

```python
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
EVENT_START = 1
Copy link

Choose a reason for hiding this comment

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

問題領域の単語をそのまま使ったほうが良いと思います。ここでは問題文中の meeting という単語を使うとよいと思います。



events.sort()
num_of_rooms = 0
Copy link

Choose a reason for hiding this comment

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

num は number of の省略形として比較的よくつかわれるらしいため、 num_rooms でよいと思います。ただし、チームでコードを書く際は、チーム内で合意形成が得られているかは確認したほうが良いと思います。

events.append((end_time, EVENT_END))

events.sort()
ongoing_meetings = 0
Copy link

Choose a reason for hiding this comment

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

ongoing_meetings という変数名ですと、開催中のミーティングを表す何らかの情報のリストを連想します。変数名に、 num・number など は入れたほうが良いと思います。したの needed_rooms も同様です。

Copy link
Owner Author

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:
        MEETING_START = 1
        MEETING_END = 0

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

@nodchip
Copy link

nodchip commented Mar 26, 2024

よいと思います。

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.

2 participants