-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path1235-maximum-profit-in-job-scheduling.js
113 lines (103 loc) · 2.85 KB
/
1235-maximum-profit-in-job-scheduling.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @param {number[]} startTime
* @param {number[]} endTime
* @param {number[]} profit
* @return {number}
*/
const jobScheduling = function (startTime, endTime, profit) {
const n = startTime.length
const items = Array(n)
for(let i = 0;i < n; i++) items[i] = [startTime[i], endTime[i], profit[i]]
items.sort((a, b) => a[1] - b[1])
const dpEndTime = [0]
const dpProfit = [0]
for(const [s, e, p] of items) {
const prevIdx = binarySearch(dpEndTime, 0, dpEndTime.length - 1, s)
const curProfit = dpProfit[prevIdx] + p, maxProfit = dpProfit[dpProfit.length - 1]
if(curProfit > maxProfit) {
dpProfit.push(curProfit)
dpEndTime.push(e)
}
}
return dpProfit[dpProfit.length - 1]
}
function binarySearch(arr, l, r, x) {
while (l < r) {
const mid = r - ((r - l) >> 1)
if (arr[mid] > x) r = mid - 1
else l = mid
}
return l
}
// another
/**
* @param {number[]} startTime
* @param {number[]} endTime
* @param {number[]} profit
* @return {number}
*/
const jobScheduling = function (startTime, endTime, profit) {
const n = startTime.length
const items = Array.from({ length: startTime.length }, () => Array(3).fill(0))
for (let i = 0; i < startTime.length; i++) {
items[i] = [startTime[i], endTime[i], profit[i]]
}
items.sort((a1, a2) => a1[1] - a2[1])
const dpProfit = [0]
for (let i = 0; i < n; i++) {
const [s, e, p] = items[i]
let prevIdx = -1
for(let j = i - 1; j >= 0; j--) {
if(items[j][1] <= items[i][0]) {
prevIdx = j
break
}
}
const curProfit = (prevIdx === -1 ? 0 : dpProfit[prevIdx]) + p
dpProfit[i] = Math.max(dpProfit[dpProfit.length - 1], curProfit)
}
return dpProfit[dpProfit.length - 1]
}
// another
/**
* @param {number[]} startTime
* @param {number[]} endTime
* @param {number[]} profit
* @return {number}
*/
const jobScheduling = function (startTime, endTime, profit) {
const items = Array.from({ length: startTime.length }, () => Array(3).fill(0))
for (let i = 0; i < startTime.length; i++) {
items[i] = [startTime[i], endTime[i], profit[i]]
}
items.sort((a1, a2) => a1[1] - a2[1])
const dpEndTime = []
const dpProfit = []
dpEndTime.push(0)
dpProfit.push(0)
for (let item of items) {
const s = item[0],
e = item[1],
p = item[2]
// find previous endTime index
const prevIdx = binarySearch(dpEndTime, 0, dpEndTime.length - 1, s)
const currProfit = dpProfit[prevIdx] + p,
maxProfit = dpProfit[dpProfit.length - 1]
if (currProfit > maxProfit) {
dpProfit.push(currProfit)
dpEndTime.push(e)
}
}
return dpProfit[dpProfit.length - 1]
}
function binarySearch(arr, l, r, x) {
while (l <= r) {
const mid = l + ((r - l) >> 1)
if (arr[mid] > x) r = mid - 1
else {
if (mid == arr.length - 1 || arr[mid + 1] > x) return mid
l = mid + 1
}
}
return -1
}