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
goyalshivank7901 hour ago
// easy c++ code
static bool comp (Job a , Job b){
        return a.profit > b.profit;}
        
    vector<int> JobScheduling(Job arr[], int n) 
    { 
        vector<bool> vis(101,false);
        sort(arr,arr+n,comp);
        int deadline = 0;
        int profit = 0;
        for(int i=0;i<n;i++){
            for(int k=arr[i].dead;k>0;k--){
                if(vis[k]==false){
                    vis[k]=arr[i].profit;
                    deadline++;
                    profit+=arr[i].profit;
                    break;}}}
        return {deadline,profit};} 
0
sauravkumar487364 hours ago
class Solution 
{
    public:
    //Function to find the maximum profit and the number of jobs done.
    static bool comp(const Job &a,const Job &b)
    {
        return a.profit>b.profit;
    }
    vector<int> JobScheduling(Job arr[], int n) 
    { 
        // your code here
        sort(arr,arr+n,comp);
        vector<int>ans;
        int count=0,profit=0;
        vector<int>deadline(101,-1);
        for(int i=0;i<n;i++)
        {
            int j=arr[i].dead;
            for(int k=j;k>0;k--)
            {
                if(deadline[k]==-1)
                {
                    deadline[k]=arr[i].profit;
                    count++;
                    profit+=arr[i].profit;
                    break;
                }
            }
        }
        ans.push_back(count);
        ans.push_back(profit);
        return ans;
    } 
};
0
hawtsauce2k2 days ago
Java | O(nlogn) Time | O(n) Space
class Solution
{
    int[] JobScheduling(Job arr[], int n)
    {
        Arrays.sort(arr, (a, b) -> a.deadline - b.deadline);
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        int time = 0, profit = 0;
        for(int i = 0; i < n; i ++) {
            int p = arr[i].profit;
            int d = arr[i].deadline;
            if(time + 1 <= d) {
                profit += p;
                pq.offer(p);
                time ++;
            } else {
                if(!pq.isEmpty() && pq.peek() < p) {
                    profit += p - pq.poll();
                    pq.offer(p);
                }
            }
        }
        return new int[]{pq.size(), profit};
        
    }
}
0
hrithikrsgupta3 days ago
t.c. O(NlogN)
 
Using path compression to find free day for the job.
 
class Solution {   public:      static bool comp(Job a,Job b){              return a.profit>b.profit;   }      int findPos(int pos,vector<int> &parent){              if(parent[pos]==0) return -1;       if(parent[pos] == pos) return parent[pos] = pos-1;              return parent[pos] = findPos(parent[pos],parent);   }   vector<int> JobScheduling(Job arr[], int n)    {                 vector<int> parent(102);                for(int i=0;i<=101;i++) parent[i] = i;                sort(arr,arr+n,comp);                int mx = 0, count = 0;                for(int i=0;i<n;i++){                        int pos = findPos(arr[i].dead,parent);                        if(pos==-1) continue;                        mx += arr[i].profit;            count++;                    }                return {count,mx};           } };
 
0
nilesh2253 days ago
class Solution{   //Function to find the maximum profit and the number of jobs done.   int[] JobScheduling(Job arr[], int n)   {       PriorityQueue<Job> pq = new PriorityQueue<>((a,b)->b.profit-a.profit);       int maxDead = 0;       for(int i=0;i<n;i++){           pq.add(arr[i]);           if(arr[i].deadline > maxDead){               maxDead = arr[i].deadline;           }       }       int[] vis = new int[maxDead+1];       int count = 0;       int maxProfit = 0;       while(!pq.isEmpty()){           Job pol = pq.poll();           for(int i=pol.deadline;i>=1;i--){               if(vis[i]==0){                   vis[i] = 1;                   count++;                   maxProfit += pol.profit;                   break;               }           }       }       return new int[] {count,maxProfit};   }}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.