-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththe_skyline_problem.go
115 lines (95 loc) · 2.5 KB
/
the_skyline_problem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"container/heap"
"sort"
)
type Edge struct {
pos int
height int
}
type PriorityQueue []*Edge
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool { return pq[i].height > pq[j].height }
func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
func (pq *PriorityQueue) Push(x interface{}) {
edge := x.(*Edge)
*pq = append(*pq, edge)
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
edge := old[n-1]
old[n-1] = nil
*pq = old[0 : n-1]
return edge
}
func getSkyline(buildings [][]int) [][]int {
n := len(buildings)
answer := [][]int{}
positions := make([][]int, 0, n*2)
for i, building := range buildings {
positions = append(positions, []int{building[0], i})
positions = append(positions, []int{building[1], i})
}
sort.Slice(positions, func(i, j int) bool {
return positions[i][0] < positions[j][0]
})
index := 0
pq := make(PriorityQueue, 0, 2*n)
heap.Init(&pq)
for index < len(positions) {
curPos := positions[index][0]
for index < len(positions) && positions[index][0] == curPos {
b := positions[index][1]
if curPos == buildings[b][0] {
heap.Push(&pq, &Edge{buildings[b][1], buildings[b][2]})
}
index++
}
for len(pq) > 0 && pq[0].pos <= curPos {
heap.Pop(&pq)
}
maxHeight := 0
if len(pq) > 0 {
maxHeight = pq[0].height
}
if len(answer) == 0 || answer[len(answer)-1][1] != maxHeight {
answer = append(answer, []int{curPos, maxHeight})
}
}
return answer
}
/*
func getSkyline(buildings [][]int) [][]int {
n := len(buildings)
answer := [][]int{}
uniqPositions := make(map[int]struct{}, n*2)
for _, building := range buildings {
uniqPositions[building[0]] = struct{}{}
uniqPositions[building[1]] = struct{}{}
}
sortedUniqPositions := make([]int, 0, len(uniqPositions))
for k, _ := range uniqPositions {
sortedUniqPositions = append(sortedUniqPositions, k)
}
sort.Ints(sortedUniqPositions)
for _, pos := range sortedUniqPositions {
maxHeight := 0
for _, building := range buildings {
if building[0] <= pos && pos < building[1] {
maxHeight = max(maxHeight, building[2])
}
}
if len(answer) == 0 || answer[len(answer)-1][1] != maxHeight {
answer = append(answer, []int{pos, maxHeight})
}
}
return answer
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
*/