Skip to content

Instantly share code, notes, and snippets.

@rtsoy
Created January 3, 2026 05:36
Show Gist options
  • Select an option

  • Save rtsoy/f688047a323450a18f6fd3f9cb7bd99b to your computer and use it in GitHub Desktop.

Select an option

Save rtsoy/f688047a323450a18f6fd3f9cb7bd99b to your computer and use it in GitHub Desktop.
56. Merge Intervals
// https://leetcode.com/problems/merge-intervals/
//
// Time: O(n log n)
// Space: O(n)
//
// n = number of intervals
// .................... //
func merge(intervals [][]int) [][]int {
sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})
res := make([][]int, 0)
for _, interval := range intervals {
if len(res) > 0 && interval[0] <= res[len(res)-1][1] {
temp := res[len(res)-1]
res = res[:len(res)-1]
interval[0] = min(temp[0], interval[0])
interval[1] = max(temp[1], interval[1])
}
res = append(res, interval)
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment