Skip to content

Commit 1716e25

Browse files
authored
Update 1235-maximum-profit-in-job-scheduling.js
1 parent 260420c commit 1716e25

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

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

+33
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
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.from({ length: startTime.length }, () => Array(3).fill(0))
10+
for (let i = 0; i < startTime.length; i++) {
11+
items[i] = [startTime[i], endTime[i], profit[i]]
12+
}
13+
items.sort((a1, a2) => a1[1] - a2[1])
14+
const dpProfit = [0]
15+
for (let i = 0; i < n; i++) {
16+
const [s, e, p] = items[i]
17+
let prevIdx = -1
18+
for(let j = i - 1; j >= 0; j--) {
19+
if(items[j][1] <= items[i][0]) {
20+
prevIdx = j
21+
break
22+
}
23+
}
24+
const curProfit = (prevIdx === -1 ? 0 : dpProfit[prevIdx]) + p
25+
dpProfit[i] = Math.max(dpProfit[dpProfit.length - 1], curProfit)
26+
}
27+
return dpProfit[dpProfit.length - 1]
28+
}
29+
30+
31+
// another
32+
33+
134
/**
235
* @param {number[]} startTime
336
* @param {number[]} endTime

0 commit comments

Comments
 (0)