Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions 252. Meeting Rooms/252. Meeting Rooms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 252. Meeting Rooms
## STEP1
- 何も見ずに解いてみる
- どこかでみたことがある。終了時刻が早い順に見ていき、それより早く開始する会議があれば参加できない。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
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

Copy link
Copy Markdown

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/

```python
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
sorted_intervals = sorted(intervals, key=lambda x: x[1])
last_end = 0
for start, end in sorted_intervals:
if start < last_end:
return False
last_end = end
return True
```

## STEP2
### プルリクやドキュメントを参照
- https://github.com/olsen-blue/Arai60/pull/56/files
- 各時刻について重複がないか累積和の考え方を使うコード。思いつかなかった。開始と終了を見れば十分なので自然かと言われると微妙なところだと思った。
- 座標圧縮をすると入力が整数でない場合も対応できる。効率化も可能。
- https://github.com/Mike0121/LeetCode/pull/27/files
- sort する際に終了時刻を基準にするか、開始時刻を基準にするかはどちらでも良いと思う。
- https://github.com/hayashi-ay/leetcode/pull/59/files
- Heap も活用できる。途中で会議が追加される場合に優位性あり。
- 下記コードは in-place で intervals をヒープにしている。
https://docs.python.org/ja/3.13/library/heapq.html#heapq.heapify
```python
import heapq


class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
heapq.heapify(intervals)
last_end = 0
while intervals:
start, end = heapq.heappop(intervals)
if start < last_end:
return False
last_end = end
return True
```
## STEP3
### 3回ミスなく書く
```python
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
intervals = sorted(intervals)
last_end = 0
for start, end in intervals:
if last_end > start:
Copy link
Copy Markdown

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
Copy Markdown

@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
Copy Markdown
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 を入れると複雑化する、どちらが主役かは明確にない、不等号は時間の流れ方向に合わせる、という点を考慮しました。

return False
last_end = end
return True
```

1分,1分,1分で3回Accept