
g0001_0100.s0056_merge_intervals.solution.go Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
package s0056_merge_intervals
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting
// #Data_Structure_II_Day_2_Array #Level_2_Day_17_Interval #Udemy_2D_Arrays/Matrix
// #Big_O_Time_O(n_log_n)_Space_O(n) #2024_03_13_Time_12_ms_(90.65%)_Space_6.3_MB_(74.98%)
import "sort"
func merge(intervals [][]int) [][]int {
sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] })
res := [][]int{intervals[0]}
for i, j := 0, 1; j < len(intervals); j++ {
if res[i][1] >= intervals[j][0] {
res[i] = []int{res[i][0], max(res[i][1], intervals[j][1])}
} else {
res = append(res, intervals[j])
i++
}
}
return res
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy