-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path1943-describe-the-painting.js
55 lines (50 loc) · 1.05 KB
/
1943-describe-the-painting.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
/**
* @param {number[][]} segments
* @return {number[][]}
*/
const splitPainting = function(segments) {
const hash = {}
for(let [s, e, c] of segments) {
if(hash[s] == null) hash[s] = 0
if(hash[e] == null) hash[e] = 0
hash[s] += c
hash[e] -= c
}
const keys = Object.keys(hash)
keys.sort((a, b) => a - b)
let prev, color = 0
const res = []
for(let k of keys) {
if(prev != null && color !== 0) res.push([prev,k,color])
prev = k
color += hash[k]
}
return res
};
// another
/**
* @param {number[][]} segments
* @return {number[][]}
*/
const splitPainting = function(segments) {
const sum = {}
for(const [s, e, v] of segments) {
if(sum[s] == null) sum[s] = 0
if(sum[e] == null) sum[e] = 0
sum[s] += v
sum[e] -= v
}
const keys = Object.keys(sum).map(e => +e)
keys.sort((a, b) => a - b)
const res = []
let pre = 0, s = 0, n = keys.length
for(let i = 0; i < n; i++) {
const k = keys[i]
if(s) {
res.push([pre, k, s])
}
s += sum[k]
pre = k
}
return res
};