-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paththe-skyline-problem.js
98 lines (89 loc) · 2.31 KB
/
the-skyline-problem.js
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
var DEBUG = process.env.DEBUG;
function Node(l, r, lo, hi) {
this.l = l;
this.r = r;
this.lo = lo;
this.hi = hi;
this.maxh = this.height = 0;
}
Node.prototype.insert = function (lo, hi, height) {
if (this.hi <= lo || this.lo >= hi) return;
if (lo <= this.lo && hi >= this.hi) {
if (this.maxh <= height) {
this.maxh = this.height = height;
return;
} else {
if ( this.height > height)
return ;
}
}
if (height >= this.maxh) this.maxh = height;
if (this.height !== -1) {
if (this.l) this.l.insert(this.lo, this.hi, this.height);
if (this.r) this.r.insert(this.lo, this.hi, this.height);
}
if (this.l) this.l.insert(lo, hi, height);
if (this.r) this.r.insert(lo, hi, height);
this.height = -1;
}
function createTree(lo, hi) {
var l, r;
var mid = Math.floor((lo+hi) / 2);
if (lo !== mid && lo + 1 !== hi) {
l = createTree(lo, mid);
}
if (hi !== mid && lo + 1 !== hi) {
r = createTree(mid, hi);
}
return new Node(l, r, lo, hi);
}
var ans, curr, num2idx, idx2num;
function dfs(node) {
if (node.height === -1 && node.maxh !== -1) {
if (node.l) dfs(node.l);
if (node.r) dfs(node.r);
return ;
}
if (node.height !== curr) {
ans.push([idx2num[node.lo], node.height]);
curr = node.height;
}
return;
}
var getSkyline = function(buildings) {
if (buildings.length === 0) return [];
var st = buildings[0][0];
var ed = 0;
buildings.forEach(function (item) { if (item[1] > ed) ed = item[1];});
var nums = [];
buildings.forEach(function (item) { nums.push(item[0]); nums.push(item[1]);});
nums.sort(function (a, b) { return a - b;});
num2idx = {};
idx2num = {};
for (var i = 0; i < nums.length; i++) {
var num = nums[i];
if (num2idx[num] === undefined) {
num2idx[num] = i;
}
var idx = num2idx[num];
idx2num[idx] = num;
}
var tree = createTree(num2idx[st], num2idx[ed]);
buildings.forEach(function (data) {
tree.insert(num2idx[data[0]], num2idx[data[1]], data[2]);
});
ans = []; curr = -1;
dfs(tree);
ans.push([ed, 0]);
return ans;
};
function test(f) {
[
[ [[3,5,10]] ],
[ [[2,9,10], [3,7,15], [5,12,12], [15,20,10], [19,24,8]] ],
[ [[2,4,7],[2,4,5],[2,4,6]] ]
].forEach(function (input) {
console.log(f.apply(undefined, input));
});
}
if (DEBUG) test(getSkyline);