Describe the Painting LeetCode solution in JavaScript

1
/**
2
* @param {number[][]} segments
3
* @return {number[][]}
4
*/
5
var splitPainting = function (segments) {
6
const arr = [];
7
segments.forEach(([start, end, val]) => {
8
arr.push([start, val]);
9
arr.push([end, -val]);
10
});
11
arr.sort((i, j) => i[0] - j[0]);
12

13
const ans = [];
14
let currVal = 0,
15
prevTime;
16
arr.forEach(([time, val]) => {
17
if (prevTime !== undefined && currVal && prevTime !== time)
18
ans.push([prevTime, time, currVal]);
19
currVal += val;
20
prevTime = time;
21
});
22

23
return ans;
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0