Skip to content

Create 252. Meeting Rooms.md#55

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

Create 252. Meeting Rooms.md#55
tokuhirat wants to merge 1 commit intomainfrom
252.-Meeting-Rooms

Conversation

@tokuhirat
Copy link
Owner

This Problem
252. Meeting Rooms
Next Ploblem
253. Meeting Rooms II
言語: Python

@tokuhirat
Copy link
Owner Author

(プレミアム問題のため、問題概要を追記します。)
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

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.

いいと思います

intervals = sorted(intervals)
last_end = 0
for start, end in intervals:
if last_end > start:

Choose a reason for hiding this comment

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

時間の流れを意識しやすそうなので、
if not last_end <= start: もありかと思います。

Copy link

@olsen-blue olsen-blue Aug 19, 2025

Choose a reason for hiding this comment

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

趣味の範囲ですが、私は条件式は、変数 < 閾値のように書くのが好きですね。
注目して見ている主役が先に来て欲しいので。

一方で、グリッド探索中の範囲内チェックでは、0 <= row < num_rowsと並び順に意味があるように書くのが便利で好きです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。あまり意識せずに書いていました。
改めて考えると
if start < last_end:
が自然に思いました。not を入れると複雑化する、どちらが主役かは明確にない、不等号は時間の流れ方向に合わせる、という点を考慮しました。

# 252. Meeting Rooms
## STEP1
- 何も見ずに解いてみる
- どこかでみたことがある。終了時刻が早い順に見ていき、それより早く開始する会議があれば参加できない。

Choose a reason for hiding this comment

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

この方法であれば出席可能な会議数の最大数を求められるので、汎用性があって良いかもです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、そこの汎用性は気が付いていませんでした。以下のような感じでしょうか。

    def count_attendable_meetings(self, intervals: List[List[int]]) -> int:
        sorted_intervals = sorted(intervals, key=lambda x: x[1])
        last_end = 0
        num_attendable_meetings = 0
        for start, end in sorted_intervals:
            if start < last_end:
                continue
            num_attendable_meetings += 1
            last_end = end
        return num_attendable_meetings

Choose a reason for hiding this comment

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

はい。良いと思います。
するとこれも解けると思います。
https://leetcode.com/problems/non-overlapping-intervals/description/

Copy link

@ryosuketc ryosuketc left a comment

Choose a reason for hiding this comment

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

LGTM です

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