Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func getSkyline(_ buildings: [[Int]]) -> [[Int]] {
- var lines = [(Int, Int, Bool)]() // index, height, isstart
- for b in buildings{
- lines.append((b[0], b[2],true))
- lines.append((b[1], b[2], false))
- }
- lines.sort{
- if $0.0 != $1.0 {return $0.0 < $1.0 }
- else if $0.2 == false { return false }
- return true
- }
- var res = [[Int]]()
- var heap = Heap<Int>(.max)
- var lastH = 0
- /**
- 最后进入结果的坐标改变必须复合这两条, 缺一不可
- 1. Index 变了
- 2. height 变了
- 所以对于每个数据点。 在index相同的时候, 我们只能丢入heap中保存
- 当Index要变化的时候, 我们才去check height变化。 - 和之前的height作比较
- **/
- for i in 0 ..< lines.count {
- var l = lines[i]
- if i != 0 && l.0 != lines[i-1].0 {
- let currentH = heap.count > 0 ? heap.peek()! : 0
- if currentH != lastH {
- res.append([lines[i-1].0, currentH])
- lastH = currentH
- }
- }
- if l.2 {
- heap.insert(l.1)
- }else {
- heap.remove(l.1)
- }
- if i == lines.count - 1 {
- res.append([l.0, 0])
- }
- }
- return res
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement