给定一个作业数组,其中每个作业都有一个截止期限,如果作业在截止期限之前完成,则可获得相关利润。此外,每个作业都占用一个单位时间,因此任何作业的最小可能截止期限都是 1。如果一次只能安排一项作业,则最大化总利润。
例子:
输入:四个工作,截止日期和利润如下
JobID 截止期限 利润
一 4 20
二 1 10
三 1 40
四 1 30
输出:以下是工作利润最大的序列:c、a
输入: 五项工作,截止日期和利润如下
JobID 截止期限 利润
a 2 100
b 1 19
c 2 27
d 1 25
e 3 15
输出:以下是工作利润最大的序列:c,a,e
朴素方法:要解决问题,请遵循以下想法:
生成给定作业集的所有子集,并检查各个子集是否可行。跟踪所有可行子集中的最大利润。
作业排序问题的贪婪方法:
贪婪地首先选择利润最高的工作,方法是按利润降序对工作进行排序。这将有助于最大化总利润,因为为每个时间段选择利润最高的工作最终将最大化总利润
按照给定的步骤解决问题:
按利润的降序对所有工作进行排序。
按利润递减的顺序对工作进行迭代。对于每项工作,执行以下操作:
找到一个时间段 i,使得时间段为空、i < 截止时间且 i 最大。将作业放入
此时间段并将此时间段标记为已填充。
如果不存在这样的 i,则忽略该工作。
下面是上述方法的实现:
// Program to find the maximum profit
// job sequence from a given array
// of jobs with deadlines and profits
// function to schedule the jobs take 2
// arguments array and no of jobs to schedule
function printJobScheduling(arr, t){
// length of array
let n = arr.length;
// Sort all jobs according to
// decreasing order of profit
for(let i=0;i<n;i++){
for(let j = 0;j<(n - 1 - i);j++){
if(arr[j][2] < arr[j + 1][2]){
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// To keep track of free time slots
let result = [];
// To store result (Sequence of jobs)
let job = [];
for(let i = 0;i<t;i++){
job[i] = '-1';
result[i] = false;
}
// Iterate through all given jobs
for(let i= 0;i<arr.length;i++){
// Find a free slot for this job
// (Note that we start from the
// last possible slot)
for(let j = (t - 1, arr[i][1] - 1);j>=0;j--){
// Free slot found
if(result[j] == false){
result[j] = true;
job[j] = arr[i][0];
break;
}
}
}
// print the sequence
document.write(job);
}
// Driver COde
arr = [['a', 2, 100], // Job Array
['b', 1, 19],
['c', 2, 27],
['d', 1, 25],
['e', 3, 15]];
document.write("Following is maximum profit sequence of jobs ");
document.write("<br>");
// Function Call
printJobScheduling(arr, 3) ;
输出
以下是工作的最大利润序列
c a e
计算机辅助设计
时间复杂度: O(N 2 )
辅助空间: O(N)
使用优先级队列(最大堆)的作业排序问题:
按截止日期的升序对作业进行排序,然后从末尾开始迭代,计算每两个连续截止日期之间的可用时隙。当空时隙可用且堆不为空时,将作业的利润包含在最大堆的根部,因为这有助于为每组可用时隙选择利润最大的作业。
下面是上述方法的实现:
// JS implementation of the above approach
// A class to represent a job
class Job {
constructor(jobId, deadline, profit) {
this.JobId = jobId;
this.Deadline = deadline;
this.Profit = profit;
}
}
function PrintJobScheduling(arr) {
let n = arr.length;
// Sorting the array based on their deadlines
arr.sort((a, b) => b.Deadline - a.Deadline);
// Initializing the result array
let result = [];
// Starting the iteration from the end
let i;
for (i = n - 1; i >= 0; i--) {
let slot_available;
// Calculating the slots between two deadlines
if (i == 0) {
slot_available = arr[i].Deadline;
} else {
slot_available = arr[i].Deadline - arr[i - 1].Deadline;
}
// Including the job with max profit
let job ;
let maxProfit = -1;
for (let j = i; j >= 0; j--) {
if (arr[j].Deadline >= slot_available && arr[j].Profit > maxProfit) {
job = arr[j];
maxProfit = arr[j].Profit;
}
}
if (job != null) {
slot_available--;
result.push(job);
arr.splice(arr.indexOf(job), 1);
i--;
// Remove the job from the input list
}
}
// Jobs included might be shuffled
// Sorting the result array based on their deadlines
result.sort((a, b) => a.Deadline - b.Deadline);
const output = result.map(job => job.JobId).join(" ");
console.log(output);
}
// Driver Code
let arr = [
new Job('a', 2, 100),
new Job('b', 1, 19),
new Job('c', 2, 27),
new Job('d', 1, 25),
new Job('e', 3, 15)
];
console.log("Following is maximum profit sequence of jobs");
// Function call
PrintJobScheduling(arr);
// This code is contributed by phasing17.
输出
以下是作业的最大利润序列
a c e
时间复杂度: O(N log N)
辅助空间: O(N)