给定一系列工作,其中每个工作都有截止日期和相关的利润,如果工作在截止日期之前完成。还假设每项工作都需要一个单位时间,因此任何工作的最小可能截止日期为 1。如果一次只能安排一项工作,如何使总利润最大化。
例子:
Input: Four Jobs with following
deadlines and profits
JobID Deadline Profit
a 4 20
b 1 10
c 1 40
d 1 30
Output: Following is maximum
profit sequence of jobs
c, a
Input: Five Jobs with following
deadlines and profits
JobID Deadline Profit
a 2 100
b 1 19
c 2 27
d 1 25
e 3 15
Output: Following is maximum
profit sequence of jobs
c, a, e
一个简单的解决方案是生成给定作业集的所有子集,并检查各个子集在该子集中作业的可行性。跟踪所有可行子集的最大利润。该解决方案的时间复杂度是指数级的。
这是一个标准的贪心算法问题。
下面是算法。
1) Sort all jobs in decreasing order of profit.
2) Iterate on jobs in decreasing order of profit.For each job , do the following :
- For each job find an empty time slot from deadline to 0. If found empty slot put the job in the slot and mark this slot filled.
下面是上述算法的实现。
C++
// Program to find the maximum profit job sequence from a given array
// of jobs with deadlines and profits
#include
#include
using namespace std;
// A structure to represent a job
struct Job
{
char id; // Job Id
int dead; // Deadline of job
int profit; // Profit if job is over before or on deadline
};
// This function is used for sorting all jobs according to profit
bool comparison(Job a, Job b)
{
return (a.profit > b.profit);
}
// Returns minimum number of platforms reqquired
void printJobScheduling(Job arr[], int n)
{
// Sort all jobs according to decreasing order of prfit
sort(arr, arr+n, comparison);
int result[n]; // To store result (Sequence of jobs)
bool slot[n]; // To keep track of free time slots
// Initialize all slots to be free
for (int i=0; i=0; j--)
{
// Free slot found
if (slot[j]==false)
{
result[j] = i; // Add this job to result
slot[j] = true; // Make this slot occupied
break;
}
}
}
// Print the result
for (int i=0; i
Java
// Program to find the maximum profit
// job sequence from a given array
// of jobs with deadlines and profits
import java.util.*;
class Job
{
// Each job has a unique-id,
// profit and deadline
char id;
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(ArrayList arr, int t)
{
// Length of array
int n = arr.size();
// Sort all jobs according to
// decreasing order of profit
Collections.sort(arr,
(a, b) -> b.profit - a.profit);
// To keep track of free time slots
boolean result[] = new boolean[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.get(i).deadline - 1);
j >= 0; j--) {
// Free slot found
if (result[j] == false)
{
result[j] = true;
job[j] = arr.get(i).id;
break;
}
}
}
// Print the sequence
for (char jb : job)
{
System.out.print(jb + " ");
}
System.out.println();
}
// Driver code
public static void main(String args[])
{
ArrayList arr = new ArrayList();
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));
// Function call
System.out.println("Following is maximum "
+ "profit sequence of jobs");
Job job = new Job();
// Calling function
job.printJobScheduling(arr, 3);
}
}
// This code is contributed by Prateek Gupta
Python3
# 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
def printJobScheduling(arr, t):
# length of array
n = len(arr)
# Sort all jobs according to
# decreasing order of profit
for i in range(n):
for j in range(n - 1 - i):
if arr[j][2] < arr[j + 1][2]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
# To keep track of free time slots
result = [False] * t
# To store result (Sequence of jobs)
job = ['-1'] * t
# Iterate through all given jobs
for i in range(len(arr)):
# Find a free slot for this job
# (Note that we start from the
# last possible slot)
for j in range(min(t - 1, arr[i][1] - 1), -1, -1):
# Free slot found
if result[j] is False:
result[j] = True
job[j] = arr[i][0]
break
# print the sequence
print(job)
# Driver COde
arr = [['a', 2, 100], # Job Array
['b', 1, 19],
['c', 2, 27],
['d', 1, 25],
['e', 3, 15]]
print("Following is maximum profit sequence of jobs")
# Function Call
printJobScheduling(arr, 3)
# This code is contributed
# by Anubhav Raj Singh
C#
// C# Program to find the maximum profit
// job sequence from a given array
// of jobs with deadlines and profits
using System;
using System.Collections.Generic;
class GFG : IComparer
{
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 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 code
static public void Main ()
{
List arr = new List();
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));
// Function call
Console.WriteLine("Following is maximum "
+ "profit sequence of jobs");
Job job = new Job();
// Calling function
job.printJobScheduling(arr, 3);
}
}
// This code is contributed by avanitracchadiya2155.
Javascript
输出
Following is maximum profit sequence of jobs
c a e
上述解决方案的时间复杂度为 O(n 2 )。它可以使用不相交集数据结构进行优化。详情请参阅以下帖子。
作业排序问题 |组 2(使用不相交组)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。