56.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. // Interval ...
  7. type Interval struct {
  8. Start int
  9. End int
  10. }
  11. // Endpoint ...
  12. type Endpoint struct {
  13. Val int
  14. IsEnd bool
  15. }
  16. // PointSlice ...
  17. type PointSlice []Endpoint
  18. func (p PointSlice) Len() int { return len(p) }
  19. func (p PointSlice) Less(i, j int) bool {
  20. // 1( 4) 4( 6) -> 1( 4( 4) 6)
  21. // important!
  22. if p[i].Val == p[j].Val {
  23. return p[j].IsEnd
  24. }
  25. return p[i].Val < p[j].Val
  26. }
  27. func (p PointSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  28. /**
  29. * Definition for an interval.
  30. * type Interval struct {
  31. * Start int
  32. * End int
  33. * }
  34. */
  35. func mergeOld(intervals []Interval) (res []Interval) {
  36. points := []Endpoint{}
  37. for _, v := range intervals {
  38. points = append(points, Endpoint{v.Start, false})
  39. points = append(points, Endpoint{v.End, true})
  40. }
  41. sort.Sort(PointSlice(points))
  42. for i, cnt := 0, 0; i < len(points); i++ {
  43. if cnt == 0 {
  44. inter := Interval{points[i].Val, 0}
  45. res = append(res, inter)
  46. }
  47. if !points[i].IsEnd {
  48. cnt++
  49. } else {
  50. cnt--
  51. if cnt == 0 {
  52. res[len(res)-1].End = points[i].Val
  53. }
  54. }
  55. }
  56. return
  57. }
  58. // By : by which function to sort intervals
  59. type By func(p, q *Interval) bool
  60. // IntervalSorter : interface for sorting intervals
  61. type IntervalSorter struct {
  62. intervals []Interval
  63. by By
  64. }
  65. func (p *IntervalSorter) Len() int {
  66. return len(p.intervals)
  67. }
  68. func (p *IntervalSorter) Less(i, j int) bool {
  69. return p.by(&p.intervals[i], &p.intervals[j])
  70. }
  71. func (p *IntervalSorter) Swap(i, j int) {
  72. p.intervals[i], p.intervals[j] = p.intervals[j], p.intervals[i]
  73. }
  74. // Sort : usage: By(func).Sort(intervals)
  75. func (by By) Sort(intervals []Interval) {
  76. is := &IntervalSorter{intervals, by}
  77. sort.Sort(is)
  78. }
  79. func startValue(p, q *Interval) bool {
  80. return p.Start < q.Start
  81. }
  82. func maxInt(x, y int) int {
  83. if x > y {
  84. return x
  85. }
  86. return y
  87. }
  88. /**
  89. * Definition for an interval.
  90. * type Interval struct {
  91. * Start int
  92. * End int
  93. * }
  94. */
  95. func merge(intervals []Interval) (res []Interval) {
  96. if len(intervals) < 2 {
  97. return intervals
  98. }
  99. By(startValue).Sort(intervals)
  100. res = append(res, intervals[0])
  101. for i := 1; i < len(intervals); i++ {
  102. if intervals[i].Start > res[len(res)-1].End {
  103. res = append(res, intervals[i])
  104. continue
  105. }
  106. res[len(res)-1].End = maxInt(intervals[i].End, res[len(res)-1].End)
  107. }
  108. return
  109. }
  110. func main() {
  111. i1 := []Interval{{1, 3}, {2, 6}, {8, 10}, {15, 18}}
  112. i2 := []Interval{}
  113. i3 := []Interval{{1, 4}, {4, 5}, {5, 6}}
  114. i4 := []Interval{{1, 4}, {0, 1}}
  115. fmt.Println(merge(i1))
  116. fmt.Println(merge(i2))
  117. fmt.Println(merge(i3))
  118. fmt.Println(merge(i4))
  119. }