forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
30 lines (30 loc) · 869 Bytes
/
Solution.ts
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
function jobScheduling(startTime: number[], endTime: number[], profit: number[]): number {
const n = startTime.length;
const f = new Array(n).fill(0);
const idx = new Array(n).fill(0).map((_, i) => i);
idx.sort((i, j) => startTime[i] - startTime[j]);
const search = (x: number) => {
let l = 0;
let r = n;
while (l < r) {
const mid = (l + r) >> 1;
if (startTime[idx[mid]] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const dfs = (i: number): number => {
if (i >= n) {
return 0;
}
if (f[i] !== 0) {
return f[i];
}
const j = search(endTime[idx[i]]);
return (f[i] = Math.max(dfs(i + 1), dfs(j) + profit[idx[i]]));
};
return dfs(0);
}