218.the-skyline-problem.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. func getSkyline(buildings [][]int) [][]int {
  2. ans := [][]int{{-1, -1}} // Prevent out of index error
  3. var sl SortedList
  4. pq := NewPQ()
  5. length := len(buildings)
  6. for i, top := 0, 0; i < length || !pq.Empty(); {
  7. if i == length || (!pq.Empty() && pq.Peek().(Pair)._1 <= buildings[i][0]) { // Delete
  8. p := pq.Dequeue().(Pair)
  9. sl.Delete(p._2)
  10. if p._1 == ans[top][0] {
  11. ans[top][1] = sl.Max()
  12. } else {
  13. ans = append(ans, []int{p._1, sl.Max()})
  14. top++
  15. }
  16. } else { // Insert
  17. l, r, h := buildings[i][0], buildings[i][1], buildings[i][2]
  18. sl.Insert(h)
  19. pq.Enqueue(Pair{r, h})
  20. i++
  21. if l == ans[top][0] {
  22. ans[top][1] = sl.Max()
  23. } else {
  24. ans = append(ans, []int{l, sl.Max()})
  25. top++
  26. }
  27. }
  28. if ans[top][1] == ans[top-1][1] { // If the height is same, pop the top of ans
  29. ans = ans[:top]
  30. top--
  31. }
  32. }
  33. return ans[1:]
  34. }
  35. type SortedList struct {
  36. List []int
  37. Len int
  38. }
  39. func (sl SortedList) Max() int {
  40. if sl.Len == 0 {
  41. return 0
  42. }
  43. return sl.List[sl.Len-1]
  44. }
  45. func (sl SortedList) Search(x int) int {
  46. beg, end := 0, sl.Len
  47. for beg < end {
  48. mid := beg + (end-beg)/2
  49. if x < sl.List[mid] {
  50. end = mid
  51. } else if sl.List[mid] < x {
  52. beg = mid + 1
  53. } else {
  54. return mid
  55. }
  56. }
  57. return beg
  58. }
  59. func (sl *SortedList) Insert(x int) {
  60. idx := sl.Search(x)
  61. sl.List = append(sl.List, 0)
  62. copy(sl.List[idx+1:], sl.List[idx:])
  63. sl.List[idx] = x
  64. sl.Len++
  65. }
  66. func (sl *SortedList) Delete(x int) {
  67. idx := sl.Search(x)
  68. copy(sl.List[idx:], sl.List[idx+1:])
  69. sl.List = sl.List[:sl.Len-1]
  70. sl.Len--
  71. }
  72. type Pair struct {
  73. _1 int
  74. _2 int
  75. }
  76. func (p Pair) Less(q Item) bool { return p._1 < q.(Pair)._1 }
  77. type PQ struct {
  78. Queue []Item
  79. Len int
  80. }
  81. type Item interface {
  82. Less(than Item) bool
  83. }
  84. func NewPQ() (pq PQ) {
  85. pq.Queue = make([]Item, 1)
  86. return
  87. }
  88. func (pq PQ) Less(i, j int) bool { return pq.Queue[i].Less(pq.Queue[j]) }
  89. func (pq PQ) Swap(i, j int) { pq.Queue[i], pq.Queue[j] = pq.Queue[j], pq.Queue[i] }
  90. func (pq PQ) Empty() bool { return pq.Len == 0 }
  91. func (pq PQ) Peek() Item { return pq.Queue[1] }
  92. func (pq *PQ) Enqueue(item Item) {
  93. pq.Queue = append(pq.Queue, item)
  94. pq.Len++
  95. pq.swim(pq.Len)
  96. }
  97. func (pq *PQ) Dequeue() Item {
  98. pq.Swap(1, pq.Len)
  99. item := pq.Queue[pq.Len]
  100. pq.Queue = pq.Queue[:pq.Len]
  101. pq.Len--
  102. pq.sink(1)
  103. return item
  104. }
  105. func (pq PQ) swim(i int) {
  106. for 1 < i && pq.Queue[i].Less(pq.Queue[i/2]) {
  107. pq.Swap(i, i/2)
  108. i /= 2
  109. }
  110. }
  111. func (pq PQ) sink(i int) {
  112. for j := 2 * i; j <= pq.Len; j *= 2 {
  113. if j+1 <= pq.Len && pq.Queue[j+1].Less(pq.Queue[j]) {
  114. j++
  115. }
  116. if !pq.Queue[j].Less(pq.Queue[i]) {
  117. break
  118. }
  119. pq.Swap(i, j)
  120. i = j
  121. }
  122. }