-
Notifications
You must be signed in to change notification settings - Fork 0
/
218.go
68 lines (61 loc) · 1.64 KB
/
218.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
package p218
import (
"sort"
)
/**
218. The Skyline Problem
https://leetcode.com/problems/the-skyline-problem/#/description
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
**/
type building struct {
l, r, h int
}
func criticalPos(buildings [][]int) []building {
res := make(map[int]struct{})
empty := struct{}{}
for _, b := range buildings {
res[b[0]] = empty
res[b[1]] = empty
}
criticals := make([]int, 0, len(res)+1)
for v := range res {
criticals = append(criticals, v)
}
sort.Ints(criticals)
ans := make([]building, len(criticals))
criticals = append(criticals, criticals[len(criticals)-1]+1)
for i := 0; i < len(ans); i++ {
ans[i] = building{criticals[i], criticals[i+1], 0}
}
return ans
}
func getSkyline(buildings [][]int) [][]int {
// get critical point(x position)
// [Li, Ri, Hi]
if len(buildings) == 0 {
return buildings
}
criticals := criticalPos(buildings)
for _, b := range buildings {
idx := sort.Search(len(criticals), func(i int) bool {
return criticals[i].l >= b[0]
})
for idx < len(criticals) && criticals[idx].l >= b[0] &&
criticals[idx].r <= b[1] {
if b[2] > criticals[idx].h {
criticals[idx].h = b[2]
}
idx++
}
}
ans := make([][]int, 0, len(criticals))
preHeight := -1
for _, b := range criticals {
if b.h == preHeight {
continue
}
ans = append(ans, []int{b.l, b.h})
preHeight = b.h
}
return ans
}