Example 1 · empty
- Arguments
- intervals
[]
- Expected return
[]
Test data
- Input
{"intervals":[]}- Expected output
[]
func MergeIntervals(intervals [][2]int) [][2]intExamples use {"intervals": [[start, end], ...]}.
The runner calls your functions directly; you do not need to parse JSON.
[]
[]
{"intervals":[]}[]
[
[
5,
8
],
[
1,
4
],
[
3,
6
]
][
[
1,
8
]
]{"intervals":[[5,8],[1,4],[3,6]]}[[1,8]]
[
[
1,
3
],
[
3,
5
],
[
8,
10
]
][
[
1,
5
],
[
8,
10
]
]{"intervals":[[1,3],[3,5],[8,10]]}[[1,5],[8,10]]
[
[
1,
10
],
[
2,
3
],
[
4,
8
]
][
[
1,
10
]
]{"intervals":[[1,10],[2,3],[4,8]]}[[1,10]]
Sorting ensures every unseen interval starts no earlier than the next interval under consideration. Once a gap appears, no later interval can connect back to the completed current interval. Taking the maximum end preserves the entire union when an interval is nested. Equality merges because endpoints belong to closed intervals.
O(n log n) time for sorting and O(n) extra space for the independent copy/output. The merge scan is O(n).
Checking execution availability…
View inputs and expected outputs. Examples do not run your code.