218.the-skyline-problem.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. func getSkyline(buildings [][]int) [][]int {
  2. return [][]int{}
  3. }
  4. type SortedList struct {
  5. List []int
  6. Len int
  7. }
  8. func (sl SortedList) Search(x int) int {
  9. beg, end := 0, sl.Len
  10. for beg < end {
  11. mid := beg + (end-beg)/2
  12. if x < sl.List[mid] {
  13. end = mid
  14. } else if sl.List[mid] < x {
  15. beg = mid + 1
  16. } else {
  17. return mid
  18. }
  19. }
  20. return beg
  21. }
  22. func (sl *SortedList) Insert(x int) {
  23. idx := sl.Search(x)
  24. sl.List = append(sl.List, 0)
  25. copy(sl.List[idx+1:], sl.List[idx:])
  26. sl.List[idx] = x
  27. sl.Len++
  28. }
  29. func (sl *SortedList) Delete(x int) {
  30. idx := sl.Search(x)
  31. copy(sl.List[idx:], sl.List[idx+1:])
  32. sl.List = sl.List[:sl.Len-1]
  33. sl.Len--
  34. }
  35. type PQ struct {
  36. Queue []Item
  37. Len int
  38. }
  39. type Item interface {
  40. Less(than Item) bool
  41. }
  42. func (pq PQ) Len() int { return pq.Len }
  43. func (pq PQ) Less(i, j int) bool { return pq.Queue[i].Less(pq.Queue[j]) }
  44. func (pq PQ) Swap(i, j int) { pq.Queue[i], pq.Queue[j] = pq.Queue[j], pq.Queue[i] }
  45. func (pq *PQ) Enqueue(item Item) {
  46. pq.Queue = append(pq.Queue, item)
  47. pq.Len++
  48. pq.swim(pq.Len)
  49. }
  50. func (pq *PQ) Dequeue() Item {
  51. pq.Swap(1, pq.Len)
  52. pq.Queue = pq.Queue[:pq.Len]
  53. pq.Len--
  54. pq.sink(pq.Len)
  55. }
  56. func (pq PQ) swim(i int) {
  57. }
  58. func (pq PQ) sink(i int) {
  59. }