Complete the code to sort intervals by their start value.
sort.Slice(intervals, func(i, j int) bool {
return intervals[i].Start [1] intervals[j].Start
})We sort intervals by their start in ascending order, so we use <.
Complete the code to check if two intervals overlap.
if intervals[i].Start [1] merged[len(merged)-1].End { // merge intervals }
Intervals overlap if the current start is less than or equal to the last merged end.
Fix the error in updating the end of the last merged interval.
merged[len(merged)-1].End = [1](merged[len(merged)-1].End, intervals[i].End)
We update the end to the maximum of the two ends to cover the full merged range.
Fill both blanks to append the first interval and start merging from the second.
merged := []Interval[1] for i := 1; i < len(intervals); i++ { // merging logic }
We start merged with the first interval intervals[0] and loop from i=1.
Fill all three blanks to complete the merge function returning merged intervals.
func merge(intervals []Interval) []Interval {
if len(intervals) == 0 {
return [1]
}
sort.Slice(intervals, func(i, j int) bool {
return intervals[i].Start [2] intervals[j].Start
})
merged := []Interval[3]
for i := 1; i < len(intervals); i++ {
if intervals[i].Start <= merged[len(merged)-1].End {
merged[len(merged)-1].End = max(merged[len(merged)-1].End, intervals[i].End)
} else {
merged = append(merged, intervals[i])
}
}
return merged
}Return empty slice if no intervals, sort ascending by start, initialize merged with first interval.
