Implemented following leetcode solution: https://leetcode.com/problems/merge-intervals/solutions/1805268/go-clean-code-with-explanation-and-visual-10ms-100
(source SlotRanges, slot SlotRange)
| 257 | // Implemented following leetcode solution: |
| 258 | // https://leetcode.com/problems/merge-intervals/solutions/1805268/go-clean-code-with-explanation-and-visual-10ms-100 |
| 259 | func AddSlotToSlotRanges(source SlotRanges, slot SlotRange) SlotRanges { |
| 260 | if len(source) == 0 { |
| 261 | return append(source, slot) |
| 262 | } |
| 263 | source = append(source, slot) |
| 264 | sort.Slice(source, func(i, j int) bool { |
| 265 | return source[i].Start < source[j].Start |
| 266 | }) |
| 267 | |
| 268 | mergedSlotRanges := make([]SlotRange, 0, len(source)) |
| 269 | mergedSlotRanges = append(mergedSlotRanges, source[0]) |
| 270 | |
| 271 | for _, interval := range source[1:] { |
| 272 | lastIntervalPos := len(mergedSlotRanges) - 1 |
| 273 | lastInterval := mergedSlotRanges[lastIntervalPos] |
| 274 | if CanMerge(lastInterval, interval) { |
| 275 | mergedSlotRanges[lastIntervalPos] = MergeSlotRanges(interval, lastInterval) |
| 276 | } else { |
| 277 | mergedSlotRanges = append(mergedSlotRanges, interval) |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return mergedSlotRanges |
| 282 | } |
| 283 | |
| 284 | func RemoveSlotFromSlotRanges(source SlotRanges, slot SlotRange) SlotRanges { |
| 285 | sort.Slice(source, func(i, j int) bool { |