邓心一 6 سال پیش
والد
کامیت
4ea5339165
1فایلهای تغییر یافته به همراه72 افزوده شده و 5 حذف شده
  1. 72 5
      hard/218.the-skyline-problem.go

+ 72 - 5
hard/218.the-skyline-problem.go

@@ -1,5 +1,36 @@
 func getSkyline(buildings [][]int) [][]int {
-	return [][]int{}
+	ans := [][]int{{-1, -1}} // Prevent out of index error
+	var sl SortedList
+	pq := NewPQ()
+	length := len(buildings)
+	for i, top := 0, 0; i < length || !pq.Empty(); {
+		if i == length || (!pq.Empty() && pq.Peek().(Pair)._1 <= buildings[i][0]) { // Delete
+			p := pq.Dequeue().(Pair)
+			sl.Delete(p._2)
+			if p._1 == ans[top][0] {
+				ans[top][1] = sl.Max()
+			} else {
+				ans = append(ans, []int{p._1, sl.Max()})
+				top++
+			}
+		} else { // Insert
+			l, r, h := buildings[i][0], buildings[i][1], buildings[i][2]
+			sl.Insert(h)
+			pq.Enqueue(Pair{r, h})
+			i++
+			if l == ans[top][0] {
+				ans[top][1] = sl.Max()
+			} else {
+				ans = append(ans, []int{l, sl.Max()})
+				top++
+			}
+		}
+		if ans[top][1] == ans[top-1][1] { // If the height is same, pop the top of ans
+			ans = ans[:top]
+			top--
+		}
+	}
+	return ans[1:]
 }
 
 type SortedList struct {
@@ -7,6 +38,13 @@ type SortedList struct {
 	Len  int
 }
 
+func (sl SortedList) Max() int {
+	if sl.Len == 0 {
+		return 0
+	}
+	return sl.List[sl.Len-1]
+}
+
 func (sl SortedList) Search(x int) int {
 	beg, end := 0, sl.Len
 	for beg < end {
@@ -37,6 +75,13 @@ func (sl *SortedList) Delete(x int) {
 	sl.Len--
 }
 
+type Pair struct {
+	_1 int
+	_2 int
+}
+
+func (p Pair) Less(q Item) bool { return p._1 < q.(Pair)._1 }
+
 type PQ struct {
 	Queue []Item
 	Len   int
@@ -46,10 +91,18 @@ type Item interface {
 	Less(than Item) bool
 }
 
-func (pq PQ) Len() int           { return pq.Len }
+func NewPQ() (pq PQ) {
+	pq.Queue = make([]Item, 1)
+	return
+}
+
 func (pq PQ) Less(i, j int) bool { return pq.Queue[i].Less(pq.Queue[j]) }
 func (pq PQ) Swap(i, j int)      { pq.Queue[i], pq.Queue[j] = pq.Queue[j], pq.Queue[i] }
 
+func (pq PQ) Empty() bool { return pq.Len == 0 }
+
+func (pq PQ) Peek() Item { return pq.Queue[1] }
+
 func (pq *PQ) Enqueue(item Item) {
 	pq.Queue = append(pq.Queue, item)
 	pq.Len++
@@ -58,16 +111,30 @@ func (pq *PQ) Enqueue(item Item) {
 
 func (pq *PQ) Dequeue() Item {
 	pq.Swap(1, pq.Len)
+	item := pq.Queue[pq.Len]
 	pq.Queue = pq.Queue[:pq.Len]
 	pq.Len--
-	pq.sink(pq.Len)
+	pq.sink(1)
+	return item
 }
 
 func (pq PQ) swim(i int) {
-	
+	for 1 < i && pq.Queue[i].Less(pq.Queue[i/2]) {
+		pq.Swap(i, i/2)
+		i /= 2
+	}
 }
 
 func (pq PQ) sink(i int) {
-	
+	for j := 2 * i; j <= pq.Len; j *= 2 {
+		if j+1 <= pq.Len && pq.Queue[j+1].Less(pq.Queue[j]) {
+			j++
+		}
+		if !pq.Queue[j].Less(pq.Queue[i]) {
+			break
+		}
+		pq.Swap(i, j)
+		i = j
+	}
 }