Repository navigation
Add consolidation cases to merge-intervals #854
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9146,6 +9146,143 @@ | |
| 10 | ||
| ] | ||
| ] | ||
| }, | ||
| { | ||
| "label": "chained overlaps: [[1,3],[2,5],[4,8]]", | ||
|
ye144440154 marked this conversation as resolved.
Resolved
|
||
| "input": [ | ||
| [ | ||
| [ | ||
| 1, | ||
| 3 | ||
| ], | ||
| [ | ||
| 2, | ||
| 5 | ||
| ], | ||
| [ | ||
| 4, | ||
| 8 | ||
| ] | ||
| ] | ||
| ], | ||
| "expected": [ | ||
| [ | ||
| 1, | ||
| 8 | ||
| ] | ||
| ] | ||
| }, | ||
| { | ||
| "label": "later window inside the first: [[1,10],[2,3],[4,5]]", | ||
|
ye144440154 marked this conversation as resolved.
Resolved
|
||
| "input": [ | ||
| [ | ||
| [ | ||
| 1, | ||
| 10 | ||
| ], | ||
| [ | ||
| 2, | ||
| 3 | ||
| ], | ||
| [ | ||
| 4, | ||
| 5 | ||
| ] | ||
| ] | ||
| ], | ||
| "expected": [ | ||
| [ | ||
| 1, | ||
| 10 | ||
| ] | ||
| ] | ||
| }, | ||
| { | ||
| "label": "four windows in one chain: [[1,3],[2,5],[4,8],[7,10]]", | ||
| "input": [ | ||
| [ | ||
| [ | ||
| 1, | ||
| 3 | ||
| ], | ||
| [ | ||
| 2, | ||
| 5 | ||
| ], | ||
| [ | ||
| 4, | ||
| 8 | ||
| ], | ||
| [ | ||
| 7, | ||
| 10 | ||
| ] | ||
| ] | ||
| ], | ||
| "expected": [ | ||
| [ | ||
| 1, | ||
| 10 | ||
| ] | ||
| ] | ||
| }, | ||
| { | ||
| "label": "overlap after a separate first window: [[1,2],[4,6],[5,7]]", | ||
| "input": [ | ||
| [ | ||
| [ | ||
| 1, | ||
| 2 | ||
| ], | ||
| [ | ||
| 4, | ||
| 6 | ||
| ], | ||
| [ | ||
| 5, | ||
| 7 | ||
| ] | ||
| ] | ||
| ], | ||
| "expected": [ | ||
| [ | ||
| 1, | ||
| 2 | ||
| ], | ||
| [ | ||
| 4, | ||
| 7 | ||
| ] | ||
| ] | ||
| }, | ||
| { | ||
| "label": "overlapping windows apart in the input: [[1,3],[5,6],[2,4]]", | ||
|
Contributor
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. An unsorted variant that folds each window into the first kept window it overlaps, then sorts the result, still passes every case here, since const merged = [];
for (const [s, e] of intervals) {
const w = merged.find(([a, b]) => s <= b && e >= a);
if (w) { w[0] = Math.min(w[0], s); w[1] = Math.max(w[1], e); }
else merged.push([s, e]);
}
return merged.sort(([a], [b]) => a - b);It never merges two kept windows with each other, so on |
||
| "input": [ | ||
| [ | ||
| [ | ||
| 1, | ||
| 3 | ||
| ], | ||
| [ | ||
| 5, | ||
| 6 | ||
| ], | ||
| [ | ||
| 2, | ||
| 4 | ||
| ] | ||
| ] | ||
| ], | ||
| "expected": [ | ||
| [ | ||
| 1, | ||
| 4 | ||
| ], | ||
| [ | ||
| 5, | ||
| 6 | ||
| ] | ||
| ] | ||
| } | ||
| ], | ||
| "paramNames": [ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.