In this problem, we are given a set of n
jobs. Each job has the following parameters:
Determine the optimal sequence of jobs that maximizes the profit while ensuring that no two jobs are executed simultaneously and each job is completed within its deadline.
Sort the jobs in descending order based on their profits. This ensures that the most profitable jobs are considered first.
Find the maximum deadline among all jobs. This determines the number of time slots available.
slots
: An array of size maxDeadline
initialized with -1
. This array tracks which job is assigned to each time slot.maxProfit
: Initialize to 0
to track the maximum profit achieved.count
: Initialize to 0
to count the number of jobs included in the optimal sequence.For each job in the sorted list:
maxProfit
.After iterating through all jobs, return:
count
: Total number of jobs included in the sequence.maxProfit
: Maximum profit achieved.jobs = [ {id: 1, deadline: 2, profit: 100}, {id: 2, deadline: 1, profit: 19}, {id: 3, deadline: 2, profit: 27}, {id: 4, deadline: 1, profit: 25}, {id: 5, deadline: 3, profit: 15} ]
[ {id: 1, profit: 100}, {id: 4, profit: 25}, {id: 3, profit: 27}, {id: 2, profit: 19}, {id: 5, profit: 15} ]
3
count = 3
(Jobs 1, 3, and 5)
maxProfit = 142
Input:
jobs = [ {id: 1, deadline: 2, profit: 50}, {id: 2, deadline: 1, profit: 40}, {id: 3, deadline: 2, profit: 60}, {id: 4, deadline: 1, profit: 20} ]
Output:
count = 2
(Jobs 3 and 1)
maxProfit = 110
Input:
jobs = [ {id: 1, deadline: 1, profit: 10}, {id: 2, deadline: 2, profit: 20}, {id: 3, deadline: 3, profit: 30} ]
Output:
count = 3
(Jobs 3, 2, and 1)
maxProfit = 60
Input:
jobs = [ {id: 1, deadline: 3, profit: 20}, {id: 2, deadline: 3, profit: 15}, {id: 3, deadline: 1, profit: 10} ]
Output:
count = 2
(Jobs 1 and 3)
maxProfit = 30
1 <= n <= 1000
1 <= profit[i], deadline[i] <= 10^4
d
is the maximum deadline)https://www.geeksforgeeks.org/problems/job-sequencing-problem-1587115620/1
Loading component...
Loading component...
INPUT: N= 4 , Jobs = {{1,4,20},{2,1,10},{3,1,40},{4,1,30}}
OUTPUT: 2,60
int maxProfit , count;
vector<int> JobScheduling(Job arr[],int n){
sort(arr,arr+n,comparison);
maxProfit = INT_MIN;
count = 0
find(arr, 0, n, 0,0,0);
return {count, maxProfit}
}//function end
void find(Job arr[],int curr , int n, int t, int profit, int cnt){
if(curr >= n){
if(profit > maxProfit){
maxProfit = profit;
count = cnt;
}
return
}
if(arr[curr].dead > t){
find(arr, curr+1, n, t+1, profit + arr[curr].profit, cnt +1);
}
find(arr, curr+1,n,t, profit,cnt);
Return;
}//function end
Utility Function is not required.