package main
import "fmt"
func main() {
intervals := [][]int{{4, 7}, {1, 3}, {2, 6}, {8, 10}, {15, 18}}
re := sortArr(intervals)
fmt.Println(intervals)
fmt.Println(re)
}
func sortArr(intervals [][]int) [][]int {
for i := 0; i < len(intervals); i++ {
for j := i + 1; j < len(intervals); j++ {
if intervals[i][0] > intervals[j][0] {
intervals[i], intervals[j] = intervals[j], intervals[i]
}
}
}
res := make([][]int, 0)
for i := 1; i < len(intervals); i++ {
if i == 1 {
res = append(res, intervals[i-1])
}
reslen := len(res) - 1
if intervals[i][0] <= res[reslen][1] {
rightRes := 0
if intervals[i][1] > res[reslen][1] {
rightRes = intervals[i][1]
} else {
rightRes = res[reslen][1]
}
res[reslen] = []int{res[reslen][0], rightRes}
} else {
res = append(res, intervals[i])
}
}
return res
}