代码拉取完成,页面将自动刷新
package list
import (
"container/heap"
"sort"
)
// Problem Definition: https://leetcode.cn/problems/the-skyline-problem/
type pair struct {
right, height int
}
type hp []pair
func (h hp) Len() int {
return len(h)
}
func (h hp) Less(i, j int) bool {
return h[i].height > h[j].height
}
func (h hp) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *hp) Push(v interface{}) {
*h = append(*h, v.(pair))
}
func (h *hp) Pop() interface{} {
a := *h
v := a[len(a)-1]
*h = a[:len(a)-1]
return v
}
func getSkyline(buildings [][]int) (ans [][]int) {
n := len(buildings)
boundaries := make([]int, 0, n*2)
for _, building := range buildings {
boundaries = append(boundaries, building[0], building[1])
}
sort.Ints(boundaries)
idx := 0
h := hp{}
for _, boundary := range boundaries {
for idx < n && buildings[idx][0] <= boundary {
heap.Push(&h, pair{right: buildings[idx][1], height: buildings[idx][2]})
idx++
}
for len(h) > 0 && h[0].right <= boundary {
heap.Pop(&h)
}
maxn := 0
if len(h) > 0 {
maxn = h[0].height
}
if len(ans) == 0 || maxn != ans[len(ans)-1][1] {
ans = append(ans, []int{boundary, maxn})
}
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。