Skip to content

Commit 822dff0

Browse files
authored
Update 1235-maximum-profit-in-job-scheduling.js
1 parent d2c01fe commit 822dff0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

1235-maximum-profit-in-job-scheduling.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
/**
2+
* @param {number[]} startTime
3+
* @param {number[]} endTime
4+
* @param {number[]} profit
5+
* @return {number}
6+
*/
7+
const jobScheduling = function (startTime, endTime, profit) {
8+
const n = startTime.length
9+
const items = Array(n)
10+
for(let i = 0;i < n; i++) items[i] = [startTime[i], endTime[i], profit[i]]
11+
items.sort((a, b) => a[1] - b[1])
12+
const dpEndTime = [0]
13+
const dpProfit = [0]
14+
for(const [s, e, p] of items) {
15+
const prevIdx = binarySearch(dpEndTime, 0, dpEndTime.length - 1, s)
16+
const curProfit = dpProfit[prevIdx] + p, maxProfit = dpProfit[dpProfit.length - 1]
17+
if(curProfit > maxProfit) {
18+
dpProfit.push(curProfit)
19+
dpEndTime.push(e)
20+
}
21+
}
22+
23+
return dpProfit[dpProfit.length - 1]
24+
}
25+
26+
function binarySearch(arr, l, r, x) {
27+
while (l < r) {
28+
const mid = r - ((r - l) >> 1)
29+
if (arr[mid] > x) r = mid - 1
30+
else l = mid
31+
}
32+
return l
33+
}
34+
35+
36+
// another
37+
138
/**
239
* @param {number[]} startTime
340
* @param {number[]} endTime

0 commit comments

Comments
 (0)