Conversation
|
(プレミアム問題のため、問題概要を追記します。) Example 1: Example 2: |
| intervals = sorted(intervals) | ||
| last_end = 0 | ||
| for start, end in intervals: | ||
| if last_end > start: |
There was a problem hiding this comment.
時間の流れを意識しやすそうなので、
if not last_end <= start: もありかと思います。
There was a problem hiding this comment.
趣味の範囲ですが、私は条件式は、変数 < 閾値のように書くのが好きですね。
注目して見ている主役が先に来て欲しいので。
一方で、グリッド探索中の範囲内チェックでは、0 <= row < num_rowsと並び順に意味があるように書くのが便利で好きです。
There was a problem hiding this comment.
コメントありがとうございます。あまり意識せずに書いていました。
改めて考えると
if start < last_end:
が自然に思いました。not を入れると複雑化する、どちらが主役かは明確にない、不等号は時間の流れ方向に合わせる、という点を考慮しました。
| # 252. Meeting Rooms | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - どこかでみたことがある。終了時刻が早い順に見ていき、それより早く開始する会議があれば参加できない。 |
There was a problem hiding this comment.
この方法であれば出席可能な会議数の最大数を求められるので、汎用性があって良いかもです。
There was a problem hiding this comment.
なるほど、そこの汎用性は気が付いていませんでした。以下のような感じでしょうか。
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_meetingsThere was a problem hiding this comment.
はい。良いと思います。
するとこれも解けると思います。
https://leetcode.com/problems/non-overlapping-intervals/description/
This Problem
252. Meeting Rooms
Next Ploblem
253. Meeting Rooms II
言語: Python