-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathWeightedJobScheduling.js
54 lines (51 loc) · 1.57 KB
/
WeightedJobScheduling.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
/**
* @author Anirudh Sharma
*
* Given N jobs where every job is represented by following three elements of it.
*
* Start Time
* Finish Time
* Profit or Value Associated (>= 0)
* Find the maximum profit subset of jobs such that no two jobs in the subset overlap.
*/
const maxProfitInJobs = (jobs) => {
// Length of the array
const n = jobs.length;
// Sort the array in ascending order by finish time
jobs.sort((a, b) => (a.endTime < b.endTime) ? -1 : 1);
// Lookup table to store the maximum profit
const lookup = new Array(n).fill(0);
// Base initialization
lookup[0] = jobs[0].profit;
// Populate the remaining table
for (let i = 1; i < jobs.length; i++) {
lookup[i] = Math.max(lookup[i - 1], jobs[i].profit);
for (let j = i - 1; j >= 0; j--) {
if (jobs[j].endTime <= jobs[i].startTime) {
lookup[i] = Math.max(lookup[i], lookup[j] + jobs[i].profit);
break;
}
}
}
// Maximum profit
let maxProfit = Number.MIN_VALUE;
for (let profit of lookup) {
maxProfit = Math.max(maxProfit, profit);
}
return maxProfit;
};
function Job(startTime, endTime, profit) {
this.startTime = (startTime === undefined ? 0 : startTime);
this.endTime = (endTime === undefined ? 0 : endTime);
this.profit = (profit === undefined ? null : profit);
}
const main = () => {
const jobs = [
new Job(3, 10, 20),
new Job(1, 2, 50),
new Job(6, 19, 100),
new Job(2, 100, 200)
];
console.log(maxProfitInJobs(jobs));
};
main();