Created
January 3, 2026 05:36
-
-
Save rtsoy/f688047a323450a18f6fd3f9cb7bd99b to your computer and use it in GitHub Desktop.
56. Merge Intervals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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