Describe the Painting LeetCode solution in JavaScript
1/**2* @param {number[][]} segments3* @return {number[][]}4*/5var splitPainting = function (segments) {6const arr = [];7segments.forEach(([start, end, val]) => {8arr.push([start, val]);9arr.push([end, -val]);10});11arr.sort((i, j) => i[0] - j[0]);1213const ans = [];14let currVal = 0,15prevTime;16arr.forEach(([time, val]) => {17if (prevTime !== undefined && currVal && prevTime !== time)18ans.push([prevTime, time, currVal]);19currVal += val;20prevTime = time;21});2223return ans;24};