1
var jobScheduling = function (startTime, endTime, profit) {2
const N = startTime.length;5
for (let i = 0; i < N; i++) {6
zip.push([startTime[i], endTime[i], profit[i]]);8
const starts = mergesort(zip, (a, b) => a[0] - b[0]).map((s) => s[0]);9
const endAsc = mergesort(zip, (a, b) => a[1] - b[1]);10
const ends = endAsc.map((s) => s[1]);12
const startToClosestEnd = {};14
for (const start of starts) {16
let nextEnd = ends[j + 1];17
// While the current and next end are before the current start...18
while (curEnd <= start && nextEnd && nextEnd <= start) {22
nextEnd = ends[j + 1];25
if (curEnd <= start) {26
startToClosestEnd[start] = curEnd;28
startToClosestEnd[start] = null;32
let maxProfit = Number.MIN_SAFE_INTEGER;35
for (const [start, end, profit] of endAsc) {36
if (startToClosestEnd[start]) {39
bestAtEnd[startToClosestEnd[start]] + profit42
maxProfit = Math.max(maxProfit, profit);45
bestAtEnd[end] = maxProfit;51
function mergesort(arr, comparator) {52
if (arr.length === 1) return arr;54
const mid = Math.floor((arr.length - 1) / 2);55
const a = mergesort(arr.slice(0, mid + 1), comparator);56
const b = mergesort(arr.slice(mid + 1), comparator);59
while (a.length || b.length) {60
if (a.length && b.length) {61
if (comparator(a[0], b[0]) < 0) {66
} else if (b.length) {