-
Notifications
You must be signed in to change notification settings - Fork 0
Merge Intervals #77
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
Yuto729
wants to merge
1
commit into
main
Choose a base branch
from
merge-intervals
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
Merge Intervals #77
Changes from all commits
Commits
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,108 @@ | ||
| # Merge Intervals | ||
| Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. | ||
|
|
||
| Example 1: | ||
| Input: intervals = [[1,3],[2,6],[8,10],[15,18]] | ||
| Output: [[1,6],[8,10],[15,18]] | ||
| Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6]. | ||
|
|
||
| Example 2: | ||
| Input: intervals = [[1,4],[4,5]] | ||
| Output: [[1,5]] | ||
| Explanation: Intervals [1,4] and [4,5] are considered overlapping. | ||
|
|
||
| Example 3: | ||
| Input: intervals = [[4,7],[1,4]] | ||
| Output: [[1,7]] | ||
| Explanation: Intervals [1,4] and [4,7] are considered overlapping. | ||
|
|
||
| ## Approach | ||
| 制約 | ||
| - 区間の要素は0以上の整数 | ||
| - 配列の長さの最大は10^4程度 | ||
| - 各要素の値の最大値も10^4程度 | ||
| - [start, end]はstart <= endが保証されている | ||
| - 空の配列は来ない | ||
|
|
||
| Meeting Rooms Iと同じように、区間配列を開始時刻で並び替えて隣同士をみる。隣同士について、start_i+1 <= end_iなら重なりがあると判定し、マージをする。マージしたら区間配列を更新する必要があるが、ここはスタックが使えそう。 | ||
| `intervals`を走査し、スタックの末尾の区間と新しい区間に重なりがある場合は、ポップしてマージした区間をスタックに追加する。重なりがない場合はそのまま追加する | ||
| マージは、[スタックの末尾の区間の始まり, スタックの末尾の区間と新しい区間の終わりのうちで大きい方]でできる | ||
|
|
||
| Time: O(N logN) ソート分 | ||
| Space: O(N) | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def merge(self, intervals: List[List[int]]) -> List[List[int]]: | ||
| sorted_intervals = sorted(intervals, key=lambda x: (x[0], x[1])) | ||
| stack = [] | ||
| for interval in sorted_intervals: | ||
| if stack and stack[-1][1] >= interval[0]: | ||
| previous = stack.pop() | ||
|
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. stack[-1][1] = max(stack[-1][1], interval[1])としたほうがシンプルだと思いました。 |
||
| stack.append(([previous[0], max(interval[1], previous[1])])) | ||
| continue | ||
|
|
||
| stack.append(interval) | ||
|
|
||
| return stack | ||
| ``` | ||
|
|
||
| test case | ||
| - [[1,3],[2,6],[8,10],[15,18]] | ||
| // init | ||
| stack = [] | ||
| // i=0 stack = [[1,3]] | ||
|
|
||
| // i=1 stack = [[1,6]] | ||
| // i=2 stack = [[1,6],[8,10]] | ||
| // i=3 stack = [[1,6],[8,10],[15,18]] return | ||
|
|
||
| - [[1,4],[4,5]] | ||
| stack = [] | ||
| // i=0 stack = [[1,4]] | ||
| // i=1 stack=[[1,5]] | ||
|
|
||
| - [[1,10],[2,3]] | ||
| - [[1,10]] | ||
|
|
||
| 別の解法 | ||
| Sweap Line(イベントベース) | ||
|
|
||
| Time: O(NlogN), Space: O(N) | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def merge(self, intervals: List[List[int]]) -> List[List[int]]: | ||
| events = [] | ||
| for start, end in intervals: | ||
| events.append((start, +1)) | ||
| events.append((end, -1)) | ||
|
|
||
| sorted_events = sorted(events, key=lambda x: (x[0], -x[1])) | ||
| current_start = 0 | ||
| count = 0 | ||
| result = [] | ||
| for time, diff in sorted_events: | ||
| if diff == 1 and count == 0: | ||
| current_start = time | ||
|
|
||
| count += diff | ||
| if count == 0: | ||
| result.append([current_start, time]) | ||
|
|
||
| return result | ||
| ``` | ||
|
|
||
| ### Follow Up | ||
|
|
||
| #### Insert Interval | ||
| マージ済み区間リストに新しい区間を挿入する問題。 | ||
| [../insert-interval/]に解いた | ||
|
|
||
| #### 動的な区間管理(カレンダーアプリ等) | ||
| 会議の追加・削除が頻繁に起きる場合、配列ベースはO(N)のシーケンシャルスキャンが毎回発生する。 | ||
|
|
||
| 平衡二分探索木(Balanced BST)を使うと挿入・削除・近傍探索がO(log N)になる。 | ||
|
|
||
| - AVL木: 左右の部分木の高さの差(バランス因子)を±1以内に保つ。厳密なバランスで検索が速い | ||
| - 赤黒木: ノードに赤・黒の色をつけてバランスを保つ。回転回数が少なく挿入・削除が速い。JavaのTreeMap、C++のstd::mapの内部実装 - Pythonでは標準ライブラリに平衡BSTがないため `sortedcontainers.SortedList` がよく使われる | ||
Oops, something went wrong.
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.
個人的には変数名にはデータ構造より、要素が何を表すかを付けることが多いです。自分なら merged_intervals と付けると思います。