Given an 2D array Jobs of size Nx3, where Jobs[i][0] represents JobID , Jobs[i][1] represents Deadline , Jobs[i][2] represents Profit associated with that job. Each Job takes 1 unit of time to complete and only one job can be scheduled at a time.
The profit associated with a job is earned only if it is completed by its deadline. Find the number of jobs and maximum profit.
Input : Jobs = [ [1, 4, 20] , [2, 1, 10] , [3, 1, 40] , [4, 1, 30] ]
Output : 2 60
Explanation : Job with JobID 3 can be performed at time t=1 giving a profit of 40.
Job with JobID 1 can be performed at time t=2 giving a profit of 20.
No more jobs can be scheduled, So total Profit = 40 + 20 => 60.
Total number of jobs completed are two, JobID 1, JobID 3.
So answer is 2 60.
Input : Jobs = [ [1, 2, 100] , [2, 1, 19] , [3, 2, 27] , [4, 1, 25] , [5, 1, 15] ]
Output : 2 127
Explanation : Job with JobID 1 can be performed at time time t=1 giving a profit of 100.
Job with JobID 3 can be performed at time t=2 giving a profit of 27.
No more jobs can be scheduled, So total Profit = 100 + 27 => 127.
Total number of jobs completed are two, JobID 1, JobID 3.
So answer is 2 127.
Input : Jobs = [ [1, 1, 100] , [2, 2, 200] , [3, 3, 300] , [4, 4, 400] ]
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to calculate maximum profit
vector<int> JobScheduling(vector<vector<int>>& Jobs) {
// Sort jobs based on profit in descending order
sort(Jobs.begin(), Jobs.end(), [](const vector<int>& a, const vector<int>& b) {
return a[2] > b[2];
});
// Total number of jobs
int n = Jobs.size();
/*Initialize a hash table
to store selected jobs.
each element represents a
deadline slot,
initially unoccupied.*/
vector<int> hash(n, -1);
// Initialize count
int cnt = 0;
// Initialize the total profit earned
int totalProfit = 0;
// Iterate over each job
for (int i = 0; i < n; i++) {
/*Iterate over each deadline slot starting
from the job's deadline*/
for (int j = Jobs[i][1] - 1; j >= 0; j--) {
/*If the current deadline
slot is available
(not occupied)*/
if (hash[j] == -1) {
// Count of selected jobs
cnt++;
// Mark the job as selected
hash[j] = Jobs[i][0];
// Update the total profit
totalProfit += Jobs[i][2];
// Move to the next job
break;
}
}
}
// Return the vector
return {cnt,totalProfit};
}
};
int main() {
vector<vector<int>> jobs = {{1, 4, 20}, {2, 1, 10}, {3, 1, 40}, {4, 1, 30}};
Solution solution;
vector<int> result = solution.JobScheduling(jobs);
// Output the result
cout << "Number of Jobs: " << result[1] << endl;
cout << "Maximum Profit: " << result[0] << endl;
return 0;
}
import java.util.*;
class Solution {
// Function to calculate maximum profit
public int[] JobScheduling(int[][] Jobs) {
// Sort jobs based on profit in descending order
Arrays.sort(Jobs, (a, b) -> b[2] - a[2]);
// Total number of jobs
int n = Jobs.length;
/*Initialize a hash table
to store selected jobs.
each element represents a
deadline slot,
initially unoccupied.*/
int[] hash = new int[n];
Arrays.fill(hash, -1);
// Initialize count
int cnt = 0;
// Initialize the total profit earned
int totalProfit = 0;
// Iterate over each job
for (int i = 0; i < n; i++) {
/*Iterate over each deadline slot starting
from the job's deadline*/
for (int j = Jobs[i][1] - 1; j >= 0; j--) {
/*If the current deadline
slot is available
(not occupied)*/
if (hash[j] == -1) {
// Count of selected jobs
cnt++;
// Mark the job as selected
hash[j] = Jobs[i][0];
// Update the total profit
totalProfit += Jobs[i][2];
// Move to the next job
break;
}
}
}
// Return the array
return new int[]{cnt,totalProfit};
}
public static void main(String[] args) {
int[][] jobs = {{1, 4, 20}, {2, 1, 10}, {3, 1, 40}, {4, 1, 30}};
Solution solution = new Solution();
int[] result = solution.JobScheduling(jobs);
// Output the result
System.out.println("Number of Jobs: " + result[1]);
System.out.println("Maximum Profit: " + result[0]);
}
}
class Solution:
# Function to calculate maximum profit
def JobScheduling(self, Jobs):
# Sort jobs based on profit in descending order
Jobs.sort(key=lambda x: x[2], reverse=True)
# Total number of jobs
n = len(Jobs)
"""Initialize a hash table
to store selected jobs.
each element represents a
deadline slot,
initially unoccupied."""
hash = [-1] * n
# Initialize count
cnt = 0
# Initialize the total profit earned
totalProfit = 0
# Iterate over each job
for i in range(n):
"""Iterate over each deadline slot starting
from the job's deadline"""
for j in range(Jobs[i][1] - 1, -1, -1):
"""If the current deadline
slot is available
(not occupied)"""
if hash[j] == -1:
# Count of selected jobs
cnt += 1
# Mark the job as selected
hash[j] = Jobs[i][0]
# Update the total profit
totalProfit += Jobs[i][2]
# Move to the next job
break
# Return the list
return [cnt,totalProfit]
if __name__ == "__main__":
jobs = [[1, 4, 20], [2, 1, 10], [3, 1, 40], [4, 1, 30]]
solution = Solution()
result = solution.JobScheduling(jobs)
# Output the result
print("Number of Jobs:", result[1])
print("Maximum Profit:", result[0])
class Solution {
// Function to calculate maximum profit
JobScheduling(Jobs) {
// Sort jobs based on profit in descending order
Jobs.sort((a, b) => b[2] - a[2]);
// Total number of jobs
const n = Jobs.length;
/*Initialize a hash table
to store selected jobs.
each element represents a
deadline slot,
initially unoccupied.*/
const hash = Array(n).fill(-1);
// Initialize count
let cnt = 0;
// Initialize the total profit earned
let totalProfit = 0;
// Iterate over each job
for (let i = 0; i < n; i++) {
/*Iterate over each deadline slot starting
from the job's deadline*/
for (let j = Jobs[i][1] - 1; j >= 0; j--) {
/*If the current deadline
slot is available
(not occupied)*/
if (hash[j] === -1) {
// Count of selected jobs
cnt++;
// Mark the job as selected
hash[j] = Jobs[i][0];
// Update the total profit
totalProfit += Jobs[i][2];
// Move to the next job
break;
}
}
}
// Return the array
return [cnt,totalProfit];
}
}
// Example usage
const jobs = [[1, 4, 20], [2, 1, 10], [3, 1, 40], [4, 1, 30]];
const solution = new Solution();
const result = solution.JobScheduling(jobs);
// Output the result
console.log("Number of Jobs: " + result[1]);
console.log("Maximum Profit: " + result[0]);
Q: Why sort jobs by profit?
A: Sorting by profit ensures that jobs with the highest reward are scheduled first, maximizing the total profit. A greedy approach works here because each job takes exactly 1 unit of time.
Q: What if a job's deadline is greater than the number of jobs?
A: Jobs with deadlines exceeding the maximum number of jobs can still be scheduled, as long as a free slot exists before their deadline. Deadlines beyond the timeline size don’t affect scheduling logic.
Q: How would you handle overlapping deadlines?
A: Overlapping deadlines are automatically handled by the greedy approach. Jobs are scheduled into the latest available slot before their respective deadlines.
Q: How would you handle jobs that take more than 1 unit of time?
A: For jobs with varying durations, modify the timeline to account for the job's duration. Ensure that consecutive slots are available before scheduling a job.