56.go 2.5 KB

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