Job Sequencing Problem | Practice | GeeksforGeeks

Given a set of N jobs where each jobi has a deadline and profit associated with it. 
Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit associated with job if and only if the job is completed by its deadline. 
Find the number of jobs done and the maximum profit.
Note: Jobs will be given in the form (Jobid, Deadline, Profit) associated with that Job.

Example 1:
Input:
N = 4
Jobs = {(1,4,20),(2,1,10),(3,1,40),(4,1,30)}
Output:
2 60
Explanation:
Job1 and Job3 can be done with
maximum profit of 60 (20+40).

Example 2:
Input:
N = 5
Jobs = {(1,2,100),(2,1,19),(3,2,27),
        (4,1,25),(5,1,15)}
Output:
2 127
Explanation:
2 jobs can be done with
maximum profit of 127 (100+27).

Your Task :
You don't need to read input or print anything. Your task is to complete the function JobScheduling() which takes an integer N and an array of Jobs(Job id, Deadline, Profit) as input and returns the count of jobs and maximum profit.

Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(N)

Constraints:
1 <= N <= 105
1 <= Deadline <= 100
1 <= Profit <= 500
0
sikkusaurav1234 days ago
class Solution {   public:   //Function to find the maximum profit and the number of jobs done.   vector<int> JobScheduling(Job arr[], int n)    {        // your code here       vector<pair<int,int>>v;       vector<int>v2;       for(int i=0;i<n;i++)       {         v.push_back(make_pair(arr[i].profit,arr[i].dead));                }       int counting=0;       int profit=0;       int k=v.size();       sort(v.begin(),v.end());       reverse(v.begin(),v.end());       int array[v.size()]={0};       for (int i=0;i<v.size();i++)       {           for(int j=min(k,v[i].second-1);j>=0;j--)           {               if(array[j]==false)               {                   counting++;                   profit=profit+v[i].first;                   array[j]=true;                   break;                                  }           }       }       v2.push_back(counting);       v2.push_back(profit);       return v2;                        } };
0
ksbsbisht1374 days ago
    static bool cmp(Job a,Job b) {       return a.profit>b.profit;   }      vector<int> JobScheduling(Job arr[], int n) {        vector<int> res;       sort(arr,arr+n,cmp);       int maxProfit=0,maxDeadLine=0,jobcount=0;       for(int i=0;i<n;i++) {           maxDeadLine=max(maxDeadLine,arr[i].dead);       }       vector<int> scheduler(maxDeadLine+1,-1);       for(int i=0;i<n;i++) {           for(int j=arr[i].dead;j>0;j--) {               if(scheduler[j]==-1) {                   scheduler[j]=arr[i].id;                   maxProfit+=arr[i].profit;                   jobcount++;                   break;               }           }       }       res.push_back(jobcount);       res.push_back(maxProfit);       return res;   } 
+1
milindprajapatmst194 days ago
Total Time Taken: 0.94/2.31
Time Complexity: O(n log n)
Space Complexity: O(n)
 
class Solution {
    public:
    static bool key(Job J1, Job J2) {
        if (J1.dead == J2.dead)
            return J1.profit > J2.profit;
        return J1.dead < J2.dead;
    }
    vector<int> JobScheduling(Job arr[], int n) { 
        sort(arr, arr + n, key);
        priority_queue<int, vector<int>, greater<int>> q;
        for (int i = 0; i < n; i++) {
            if (q.size() < arr[i].dead)
                q.push(arr[i].profit);
            else if (q.top() < arr[i].profit) {
                q.pop();
                q.push(arr[i].profit);
            }
        }
        int cnt = 0, profit = 0;
        while (!q.empty()) {
            cnt++;
            profit += q.top();
            q.pop();
        }
        return {cnt, profit};
    } 
};
0
amanyddv2 weeks ago
vector<int> JobScheduling(Job arr[], int n)    {        int count=0;       int profit=0;       vector<int> ans;       int m=arr[0].dead;       for(int i=1;i<n;i++){           m=max(m,arr[i].dead);       }       vector<int> c;       for(int i=0;i<=m;i++){           c.push_back(0);       }       sort(arr,arr+n,[](Job j1,Job j2){           return j1.profit>j2.profit;       });              for(int i=0;i<n;i++){          int  d=arr[i].dead;           while(d>0){               if(c[d]==0){                   c[d]=1;                   count++;                   profit+=arr[i].profit;                   break;               }               d--;           }                  }       ans.push_back(count);       ans.push_back(profit);              return ans;   } 
0
sankt2 weeks ago
JAVA SOLUTION
 
class cmp implements Comparator<Job>
{
    public int compare(Job a, Job b) {
        return b.profit - a.profit;
    }
}
class Solution
{
    int[] JobScheduling(Job arr[], int n)
    {
        boolean[] slot = new boolean[101];
        Arrays.sort(arr, new cmp());
        
        int jobCount = 0, profitCount = 0;
        
        for (int i=0; i<n; i++) {
            
            Job currJob = arr[i];
            
            int index = 0;
            
            if (currJob.deadline < n) {
                index = currJob.deadline-1;
            } else {
                index = n - 1;
            }
            
            while (index >= 0) {
                
                if (slot[index] == false) {
                    slot[index] = true;
                    jobCount++;
                    profitCount += currJob.profit;
                    break;
                }
                
                index--;
            }
        }
        
        int[] list = {jobCount, profitCount};
        return list;
    }
}
0
jainmuskan5652 weeks ago
vector<int> JobScheduling(Job arr[], int n)    {        vector<int> jobSlots(100,-1);       // sort the arr descending order according to profit       sort(arr,arr+n,comp);       // max profit and iterate over the sorted arr       int maxProfit=0;       int maxJobs=0;       for(int i=0;i<n;i++){           for(int j=min(n,arr[i].dead);j>=0;j--){               if(jobSlots[j -1]== -1){               maxProfit+= arr[i].profit;               maxJobs++;               jobSlots[j-1]= arr[i].dead;               break;               }           }                  }       vector<int> res;       res.push_back(maxJobs);       res.push_back(maxProfit);       return res;   } 
0
madhurjyasaha3 weeks ago
class Solution 
{
    public:
    //Function to find the maximum profit and the number of jobs done.
    vector<int> JobScheduling(Job arr[], int n) 
    { 
        int maxDeadline = INT_MIN;
        for(int i=0; i<n; i++)
            maxDeadline = max(maxDeadline, arr[i].dead);
        
        // job schedule    
        vector<int> schedule(maxDeadline+1, -1);
        int jobCount = 0;
        int totalProfit = 0;
        
        // Sort jobs in decreasing order of their profits
        sort(arr, arr+n, [](Job j1, Job j2){
            return j1.profit > j2.profit;
        });
        
        // Pick a job from decreasing order of profit, and place the job as as late as possible within its deadline
        for(int i=0; i<n; i++)
        {
            for(int j=arr[i].dead; j>=1; j--)
            {
                if(schedule[j] == -1)
                {
                    schedule[j] = arr[i].id;
                    jobCount++;
                    totalProfit += arr[i].profit;
                    break;
                }
            }
        }
        // Job Sequence
        // cout<<"Day \tJob id"<<endl;
        // for(int i=1; i<schedule.size(); i++)
        // {
        //     if(schedule[i] == -1)
        //         cout<<i<<'\t'<<"NO WORK--ENJOY!"<<endl;
        //     else    
        //         cout<<i<<'\t'<<schedule[i]<<endl;
        // }
        
        vector<int> res = {jobCount, totalProfit};
        return res;
    } 
};
+1
saurabhsathe12343 weeks ago
C++ : 1.1 
 
static bool cmp(Job a , Job b)
    {
        if(a.profit == b.profit)
            return a.dead < b.dead;
            
            
        return a.profit > b.profit;
    }
    vector<int> JobScheduling(Job arr[], int n) 
    { 
        // your code here
        sort(arr,arr+n,cmp);
        vector<int> res(n,-1);
        int pro = 0 , count = 0 ;
        for(int i=0 ; i<n ; i++)
        {
            for(int j = arr[i].dead - 1  ; j>=0 ; j--)
            {
                if(res[j] == -1)
                {
                    res[j] = arr[i].id;
                    count++;
                    pro += arr[i].profit;
                    break;
                }
            }
        }
        
        vector<int> v;
        v.push_back(count);
        v.push_back(pro);
        
        return v;
        
    } 
0
dev1714 weeks ago
C++ SIMPLE GREEDY
   vector<int> JobScheduling(Job arr[], int n) 
    { 
  
        int mxd=0;
        for(int i=0;i<n;i++){
            mxd=max(mxd,arr[i].dead);   
        }
        vector<int>timeSlot(mxd+1,0);
        sort(arr,arr+n,[](Job &a,Job &b){
            return a.profit>b.profit;
        });
        int Tprofit=0,job=0;
        for(int i=0;i<n;i++){
            int deadline=arr[i].dead;
            int profit=arr[i].profit;
        
            for(int i=timeSlot.size()-1;i>0;i--){
                if(timeSlot[i]==0 && i<=deadline){
                    timeSlot[i]=1;
                    job++;
                    Tprofit+=profit;
                    break;
                }
            }
        }
        return {job,Tprofit};
    } 
+2
bhardwajbhairavnath1 month ago
/*Approach:-Greedy Approach +Sorting.-->Initially sort the array in Decreasing order of Profit.-->Make a Slot array to store profit for particular slot.-->How we will store :-We can store the Particular job profit in btween Slot 0 to deadline Slot.But for maximum profit we will store it in the maximum slot btween 0 to deadline which is Vaccent.
Note:-In Code Deadline Slot is Limit slot.*/
 
bool cmp(Job x,Job y)
{
   /*Comprator function to sort the array in decreasing order.*/
   return x.profit>y.profit;
}
class Solution 
{
   public:
   //Function to find the maximum profit and the number of jobs done.
   vector<int> JobScheduling(Job arr[], int n) 
   { 
       vector<int>slot(100,0); //Deadline constrain is 100 ,so i make a slot array of 100.
       
       sort(arr,arr+n,cmp); //Sort in Decreasing Order on the basis of profit.
       
       int count=0,max_profit=0;
       
       
       for(int i=0;i<n;i++)
       {
           int limit=arr[i].dead-1; // Limit is out Deadline period.
           
           for(int j=limit;j>=0;j--) //We traverse in Backward and when we find any vacent slot we Put that job in vacent slot.
           {
               if(slot[j]==0)
               {
                 slot[j]=arr[i].profit;
                 count++; //Increment the count.
                 max_profit+=arr[i].profit; //Update the Profit.
                 break; //Break the J loop.
               }
           }
       }
       
       return {count,max_profit};
   } 
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.