diff --git a/252_meeting_rooms_easy/README.md b/252_meeting_rooms_easy/README.md new file mode 100644 index 0000000..5b348c4 --- /dev/null +++ b/252_meeting_rooms_easy/README.md @@ -0,0 +1,52 @@ +# 問題へのリンク +[Meeting Rooms - LeetCode](https://leetcode.com/problems/meeting-rooms/) + +# 言語 +Python + +# 問題の概要 + +ある人が複数の会議に出席できるかを判定する問題である。 + +* 各会議は時間帯 `[starti, endi]` で表される。 +* 入力は、こうした会議の時間帯のリスト `intervals` で与えられる。 +* **すべての会議に出席するには、会議同士が重ならない必要がある。** + +#### 例1: + +``` +入力: [[0,30],[5,10],[15,20]] +出力: false(0〜30と5〜10が重なるため) +``` + +#### 例2: + +``` +入力: [[7,10],[2,4]] +出力: true(会議が重ならないため) +``` + +#### 制約: + +* 会議数は 0〜10,000 件 +* 各会議の時間帯は `[starti, endi]`(開始 < 終了、かつ0以上1,000,000以下) + + +# 自分の解法 +`intervals` の時間帯を開始時間でソートし、隣接する会議の終了時間と開始時間を比較する方法。ひとつでも重なっている会議があれば、`False` を返す。 +- 時間計算量:`O(nlogn)` +- 空間計算量:`O(1)` + +## step2 + +- `start`, `end` の変数名を `start_time`, `end_time` に変更して、より明確にした。 + +# 別解・模範解答 +`intervals` からすべての時間帯のペアを取り出し、そのペアが重なるかどうかを確認する方法。 + +- 時間計算量:`O(n^2)`(すべてのペアを確認するため) +- 空間計算量:`O(1)` + +# 次に解く問題の予告 +- [Largest Component Size by Common Factor - LeetCode](https://leetcode.com/problems/largest-component-size-by-common-factor/description/) +- [Longest Consecutive Sequence - LeetCode](https://leetcode.com/problems/longest-consecutive-sequence/description/) diff --git a/252_meeting_rooms_easy/another_solution.py b/252_meeting_rooms_easy/another_solution.py new file mode 100644 index 0000000..ccb34ae --- /dev/null +++ b/252_meeting_rooms_easy/another_solution.py @@ -0,0 +1,31 @@ +# +# @lc app=leetcode id=252 lang=python3 +# +# [252] Meeting Rooms +# + + +# @lc code=start +TIME_POINT_START = 1 +TIME_POINT_END = -1 + + +class Solution: + def canAttendMeetings(self, intervals: list[list[int]]) -> bool: + time_points: list[tuple[int, int]] = [] + for start_time, end_time in intervals: + time_points.append((start_time, TIME_POINT_START)) + time_points.append((end_time, TIME_POINT_END)) + + time_points.sort() + # time_points must have start time and end time in alternating order after sorting + # if not, then there are overlapping intervals + previous_time_type = TIME_POINT_END + for _, time_type in time_points: + if time_type == previous_time_type: + return False + previous_time_type = time_type + return True + + +# @lc code=end diff --git a/252_meeting_rooms_easy/bruteforce.py b/252_meeting_rooms_easy/bruteforce.py new file mode 100644 index 0000000..b092365 --- /dev/null +++ b/252_meeting_rooms_easy/bruteforce.py @@ -0,0 +1,31 @@ +# +# @lc app=leetcode id=252 lang=python3 +# +# [252] Meeting Rooms +# + + +# @lc code=start +class Solution: + def canAttendMeetings(self, intervals: list[list[int]]) -> bool: + + def are_overlapping( + start_time1: int, + end_time1: int, + start_time2: int, + end_time2: int, + ) -> bool: + if start_time1 < end_time1 <= start_time2 < end_time2: + return False + elif start_time2 < end_time2 <= start_time1 < end_time1: + return False + return True + + for i in range(len(intervals) - 1): + for j in range(i + 1, len(intervals)): + if are_overlapping(*intervals[i], *intervals[j]): + return False + return True + + +# @lc code=end diff --git a/252_meeting_rooms_easy/step1.py b/252_meeting_rooms_easy/step1.py new file mode 100644 index 0000000..91c4da3 --- /dev/null +++ b/252_meeting_rooms_easy/step1.py @@ -0,0 +1,22 @@ +# +# @lc app=leetcode id=252 lang=python3 +# +# [252] Meeting Rooms +# + + +# @lc code=start + + +class Solution: + def canAttendMeetings(self, intervals: list[list[int]]) -> bool: + intervals.sort() + previous_end = 0 + for start, end in intervals: + if start < previous_end: + return False + previous_end = end + return True + + +# @lc code=end diff --git a/252_meeting_rooms_easy/step2.py b/252_meeting_rooms_easy/step2.py new file mode 100644 index 0000000..d9ac9ac --- /dev/null +++ b/252_meeting_rooms_easy/step2.py @@ -0,0 +1,20 @@ +# +# @lc app=leetcode id=252 lang=python3 +# +# [252] Meeting Rooms +# + + +# @lc code=start +class Solution: + def canAttendMeetings(self, intervals: list[list[int]]) -> bool: + intervals.sort() + previous_end_time = 0 + for start_time, end_time in intervals: + if start_time < previous_end_time: + return False + previous_end_time = end_time + return True + + +# @lc code=end diff --git a/252_meeting_rooms_easy/step3.py b/252_meeting_rooms_easy/step3.py new file mode 100644 index 0000000..be1fea0 --- /dev/null +++ b/252_meeting_rooms_easy/step3.py @@ -0,0 +1,22 @@ +# +# @lc app=leetcode id=252 lang=python3 +# +# [252] Meeting Rooms +# + + +# @lc code=start + + +class Solution: + def canAttendMeetings(self, intervals: list[list[int]]) -> bool: + intervals.sort() + previous_end_time = 0 + for start_time, end_time in intervals: + if start_time < previous_end_time: + return False + previous_end_time = end_time + return True + + +# @lc code=end