-
Notifications
You must be signed in to change notification settings - Fork 0
Solve 252_meeting_rooms_easy #12
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
Open
Kaichi-Irie
wants to merge
2
commits into
main
Choose a base branch
from
252_meeting_rooms_easy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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/) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return max(start_time1, start_time2) < min(end_time1, end_time2)と書けると思いました。ただ、ややパズルに感じられるため、元のコードのほうがよいかもしれません。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. レビューありがとうございます。 |
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
他に、(開始時間, 1)、(終了時間, -1) と書かれた紙を箱に入れて、小さい順に取り出して、開始と終了が交互に出てくるかどうかを確認するという方法もありそうですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
確かにそうですね、早速実装してみました。