Skip to content

Commit a2bae0f

Browse files
add 218
1 parent 4e5ee37 commit a2bae0f

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ LeetCode
195195
|0215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | c | [c++](./src/0215-Kth-Largest-Element-in-an-Array/0215.cpp) |[python](./src/0215-Kth-Largest-Element-in-an-Array/0215.py)|||Medium|
196196
|0216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/) | c | [c++](./src/0216-Combination-Sum-III/0216.cpp) |[python](./src/0216-Combination-Sum-III/0216.py)|||Medium|
197197
|0217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | c | [c++](./src/0217-Contains-Duplicate/0217.cpp) |[python](./src/0217-Contains-Duplicate/0217.py)|||Easy|
198+
|0218|[The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/) | c | [c++](./src/0218-The-Skyline-Problem/0218.cpp) |[python](./src/0218-The-Skyline-Problem/0218.py)|[go](./src/0218-The-Skyline-Problem/0218.go)|[js](./src/0218-The-Skyline-Problem/0218.js)|Hard|
198199
|0219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | c | [c++](./src/0219-Contains-Duplicate-II/0219.cpp) |[python](./src/0219-Contains-Duplicate-II/0219.py)|||Easy|
199200
|0221|[Maximal Square](https://leetcode.com/problems/maximal-square/) | c | [c++](./src/0221-Maximal-Square/0221.cpp) |[python](./src/0221-Maximal-Square/0221.py)|[go](./src/0221-Maximal-Square/0221.go)|[js](./src/0221-Maximal-Square/0221.js)|Medium|
200201
|0222|[Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/) | c | [c++](./src/0222-Count-Complete-Tree-Nodes/0222.cpp) |[python](./src/0222-Count-Complete-Tree-Nodes/0222.py)|||Medium|

src/0218-The-Skyline-Problem/0218.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution
2+
{
3+
public:
4+
vector<vector<int>> getSkyline(vector<vector<int>>& buildings)
5+
{
6+
vector<vector<int> > res;
7+
vector<pair<int, int> > points;
8+
9+
for(const auto& it : buildings)
10+
{
11+
points.push_back({it[0], -it[2]});
12+
points.push_back({it[1], it[2]});
13+
}
14+
15+
sort(points.begin(), points.end());
16+
multiset<int> height = {0};
17+
int preHighest = 0;
18+
int curHighest = 0;
19+
for(const auto& co : points)
20+
{
21+
if(co.second < 0) height.insert(-co.second);
22+
else height.erase(height.find(co.second));
23+
curHighest = *height.rbegin();
24+
if(preHighest != curHighest)
25+
{
26+
res.push_back({co.first, curHighest});
27+
preHighest = curHighest;
28+
}
29+
}
30+
return res;
31+
}
32+
};

src/0218-The-Skyline-Problem/0218.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
type IntHeap [][]int
2+
3+
func (h IntHeap) Len() int {
4+
return len(h)
5+
}
6+
7+
func (h IntHeap) Less(i, j int) bool {
8+
if h[i][0] == h[j][0] {
9+
return h[i][1] < h[j][1]
10+
}
11+
return h[i][0] < h[j][0]
12+
}
13+
14+
func (h IntHeap) Swap(i, j int) {
15+
h[i], h[j] = h[j], h[i]
16+
}
17+
18+
func (h *IntHeap) Pop() interface{} {
19+
x := (*h)[(*h).Len()-1]
20+
*h = (*h)[:(*h).Len()-1]
21+
return x
22+
}
23+
24+
func (h *IntHeap) Push(x interface{}) {
25+
*h = append(*h, x.([]int))
26+
}
27+
28+
func getSkyline(buildings [][]int) [][]int {
29+
const INT_MAX = int(^uint(0) >> 1)
30+
points := make([][]int, 0)
31+
rSet := make(map[int]int)
32+
for _, v := range buildings {
33+
points = append(points, []int{v[0], -v[2], v[1]})
34+
if _, ok := rSet[v[1]]; !ok {
35+
points = append(points, []int{v[1], 0, 0})
36+
rSet[v[1]] = 1
37+
}
38+
}
39+
sort.Slice(points, func(a, b int) bool {
40+
if points[a][0] == points[b][0] {
41+
return points[a][1] < points[b][1]
42+
}
43+
return points[a][0] < points[b][0]
44+
})
45+
h := new(IntHeap)
46+
heap.Push(h, []int{0, INT_MAX})
47+
res := [][]int{{0, 0}}
48+
for _, v := range points {
49+
for (*h)[0][1] <= v[0] {
50+
heap.Pop(h)
51+
}
52+
if v[1] != 0 {
53+
heap.Push(h, []int{v[1], v[2]})
54+
}
55+
if res[len(res) - 1][1] != -(*h)[0][0] {
56+
res = append(res, []int{v[0], -(*h)[0][0]})
57+
}
58+
}
59+
return res[1:len(res)]
60+
}

src/0218-The-Skyline-Problem/0218.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const t = 0;
2+
const parent = i => ((i + 1) >>> 1) - 1;
3+
const left = i => (i << 1) + 1;
4+
const right = i => (i + 1) << 1;
5+
6+
class PriorityQueue {
7+
constructor(comparator = (a, b) => a > b) {
8+
this._heap = [];
9+
this._comparator = comparator;
10+
}
11+
12+
size() {
13+
return this._heap.length;
14+
}
15+
16+
isEmpty() {
17+
return this.size() == 0;
18+
}
19+
20+
peek() {
21+
return this._heap[t];
22+
}
23+
24+
push(...values) {
25+
values.forEach(value => {
26+
this._heap.push(value);
27+
this._siftUp();
28+
});
29+
return this.size();
30+
}
31+
32+
pop() {
33+
const poppedValue = this.peek();
34+
const bottom = this.size() - 1;
35+
if (bottom > t) {
36+
this._swap(t, bottom);
37+
}
38+
this._heap.pop();
39+
this._siftDown();
40+
return poppedValue;
41+
}
42+
43+
replace(value) {
44+
const replacedValue = this.peek();
45+
this._heap[t] = value;
46+
this._siftDown();
47+
return replacedValue;
48+
}
49+
50+
_greater(i, j) {
51+
return this._comparator(this._heap[i], this._heap[j]);
52+
}
53+
54+
_swap(i, j) {
55+
[this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
56+
}
57+
58+
_siftUp() {
59+
let node = this.size() - 1;
60+
while (node > t && this._greater(node, parent(node))) {
61+
this._swap(node, parent(node));
62+
node = parent(node);
63+
}
64+
}
65+
66+
_siftDown() {
67+
let node = t;
68+
while (
69+
(left(node) < this.size() && this._greater(left(node), node)) ||
70+
(right(node) < this.size() && this._greater(right(node), node))
71+
) {
72+
let maxChild = (right(node) < this.size() && this._greater(right(node), left(node))) ? right(node) : left(node);
73+
this._swap(node, maxChild);
74+
node = maxChild;
75+
}
76+
}
77+
}
78+
/**
79+
* @param {number[][]} buildings
80+
* @return {number[][]}
81+
*/
82+
var getSkyline = function(buildings) {
83+
let points = [], rSet = new Set();
84+
for (let it of buildings) {
85+
points.push([it[0], -it[2], it[1]]);
86+
if (!rSet.has(it[1])) {
87+
points.push([it[1], 0, 0])
88+
rSet.add(it[1]);
89+
}
90+
}
91+
let cmp = (a, b) => {
92+
if (a[0] == b[0]) return a[1] - b[1];
93+
return a[0] - b[0];
94+
}
95+
points.sort(cmp);
96+
const q = new PriorityQueue(cmp);
97+
q.push([0, Number.MAX_VALUE])
98+
let res = [[0, 0]];
99+
for (let it of points) {
100+
while (q.peek()[1] <= it[0]) q.pop();
101+
if (it[1] != 0) q.push([it[1], it[2]]);
102+
if (res[res.length-1][1] != -q.peek()[0])
103+
res.push([it[0], -q.peek()[0]]);
104+
}
105+
res.shift();
106+
return res;
107+
};

src/0218-The-Skyline-Problem/0218.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import heapq
2+
class Solution:
3+
def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]:
4+
points = [(L, -H, R) for L, R, H in buildings] + [(R, 0, 0) for R in set(r for _, r, _ in buildings)]
5+
points.sort()
6+
heap, res = [(0, float('inf'))], [[0, 0]]
7+
for l, nh, r in points:
8+
while heap[0][1] <= l:
9+
heapq.heappop(heap)
10+
if nh:
11+
heapq.heappush(heap, (nh, r))
12+
if res[-1][1] != -heap[0][0]:
13+
res += [[l, -heap[0][0]]]
14+
return res[1:]

0 commit comments

Comments
 (0)