给定一个作业数组,其中每个作业都有一个截止期限,如果作业在截止期限之前完成,则可获得相关利润。此外,每个作业都占用一个单位时间,因此任何作业的最小可能截止期限都是 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,则忽略该工作。
下面是上述方法的实现:
// C# Program for the above approach
using System;
using System.Collections.Generic;
class GFG : IComparer<Job> {
public int Compare(Job x, Job y)
{
if (x.profit == 0 || y.profit == 0) {
return 0;
}
// CompareTo() method
return (y.profit).CompareTo(x.profit);
}
}
public class Job {
// Each job has a unique-id,
// profit and deadline
char id;
public int deadline, profit;
// Constructors
public Job() {}
public Job(char id, int deadline, int profit)
{
this.id = id;
this.deadline = deadline;
this.profit = profit;
}
// Function to schedule the jobs take 2
// arguments arraylist and no of jobs to schedule
void printJobScheduling(List<Job> arr, int t)
{
// Length of array
int n = arr.Count;
GFG gg = new GFG();
// Sort all jobs according to
// decreasing order of profit
arr.Sort(gg);
// To keep track of free time slots
bool[] result = new bool[t];
// To store result (Sequence of jobs)
char[] job = new char[t];
// Iterate through all given jobs
for (int i = 0; i < n; i++) {
// Find a free slot for this job
// (Note that we start from the
// last possible slot)
for (int j
= Math.Min(t - 1, arr[i].deadline - 1);
j >= 0; j--) {
// Free slot found
if (result[j] == false) {
result[j] = true;
job[j] = arr[i].id;
break;
}
}
}
// Print the sequence
foreach(char jb in job) { Console.Write(jb + " "); }
Console.WriteLine();
}
// Driver's code
static public void Main()
{
List<Job> arr = new List<Job>();
arr.Add(new Job('a', 2, 100));
arr.Add(new Job('b', 1, 19));
arr.Add(new Job('c', 2, 27));
arr.Add(new Job('d', 1, 25));
arr.Add(new Job('e', 3, 15));
Console.WriteLine("Following is maximum "
+ "profit sequence of jobs");
Job job = new Job();
// Function call
job.printJobScheduling(arr, 3);
}
}
// This code is contributed by avanitracchadiya2155.
输出
以下是工作的最大利润序列
c a e
计算机辅助设计
时间复杂度: O(N 2 )
辅助空间: O(N)
使用优先级队列(最大堆)的作业排序问题:
按截止日期的升序对作业进行排序,然后从末尾开始迭代,计算每两个连续截止日期之间的可用时隙。当空时隙可用且堆不为空时,将作业的利润包含在最大堆的根部,因为这有助于为每组可用时隙选择利润最大的作业。
下面是上述方法的实现:
// C# implementation of the above approach
using System;
using System.Collections.Generic;
namespace GFG
{
// A class to represent a job
public class Job
{
public char JobId { get; set; }
public int Deadline { get; set; }
public int Profit { get; set; }
public Job(char jobId, int deadline, int profit)
{
this.Deadline = deadline;
this.JobId = jobId;
this.Profit = profit;
}
}
class Scheduling
{
static void PrintJobScheduling(List<Job> arr)
{
int n = arr.Count;
// Sorting the array based on their deadlines
arr.Sort((a, b) => b.Deadline.CompareTo(a.Deadline));
// Initializing the result array
List<Job> result = new List<Job>();
// Starting the iteration from the end
for (int i = n - 1; i >= 0; i--)
{
int 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
Job job = null;
int maxProfit = -1;
for (int 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.Add(job);
arr.Remove(job);
i--;
}
}
// Jobs included might be shuffled
// Sorting the result array based on their deadlines
result.Sort((a, b) => a.Deadline.CompareTo(b.Deadline));
foreach (Job job in result)
{
Console.Write(job.JobId + " ");
}
Console.WriteLine();
}
// Driver Code
static void Main(string[] args)
{
List<Job> arr = new List<Job>
{
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.WriteLine("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)