853. Most Profit Assigning Work
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the maximum total profit we can achieve by assigning jobs to a group of workers. Each job has a specific difficulty and profit. Each worker has an ability, which is the maximum difficulty of a job they can perform. A key detail is that each worker can take at most one job, but a single job can be performed by multiple workers. For each worker, we should assign them the most profitable job they are capable of doing.
Solution
Most Profit Assigning Work in Python
class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
# Create a list of jobs as (difficulty, profit) tuples and sort by difficulty
jobs = sorted(zip(difficulty, profit))
# Sort workers by their ability in ascending order
worker.sort()
total_profit = 0
max_profit_so_far = 0
job_index = 0
n = len(jobs)
# Iterate through each worker, sorted by ability
for w in worker:
# While the worker can do the job at job_index, update max_profit_so_far.
# Because workers are sorted, job_index and max_profit_so_far are non-decreasing.
while job_index < n and w >= jobs[job_index][0]:
max_profit_so_far = max(max_profit_so_far, jobs[job_index][1])
job_index += 1
# Assign the best possible job profit to the current worker
total_profit += max_profit_so_far
return total_profit
Most Profit Assigning Work in Java
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int n = difficulty.length;
// Use a 2D array to store jobs as [difficulty, profit]
int[][] jobs = new int[n][2];
for (int i = 0; i < n; i++) {
jobs[i][0] = difficulty[i];
jobs[i][1] = profit[i];
}
// Sort jobs by difficulty in ascending order
Arrays.sort(jobs, (a, b) -> a[0] - b[0]);
// Sort workers by ability in ascending order
Arrays.sort(worker);
int totalProfit = 0;
int maxProfitSoFar = 0;
int jobIndex = 0;
// Iterate through each worker, sorted by ability
for (int w : worker) {
// Update maxProfitSoFar with any new jobs the current worker can do
// Because workers are sorted, jobIndex and maxProfitSoFar only increase
while (jobIndex < n && w >= jobs[jobIndex][0]) {
maxProfitSoFar = Math.max(maxProfitSoFar, jobs[jobIndex][1]);
jobIndex++;
}
// Add the best possible profit for this worker to the total profit
totalProfit += maxProfitSoFar;
}
return totalProfit;
}
}
Most Profit Assigning Work in C++
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
// Create pairs of (difficulty, profit) to associate them
vector<pair<int, int>> jobs;
for (int i = 0; i < difficulty.size(); ++i) {
jobs.push_back({difficulty[i], profit[i]});
}
// Sort jobs by difficulty in ascending order
sort(jobs.begin(), jobs.end());
// Sort workers by ability in ascending order
sort(worker.begin(), worker.end());
int totalProfit = 0;
int maxProfitSoFar = 0;
int jobIndex = 0;
// Iterate through each worker, sorted by ability
for (int w : worker) {
// Update maxProfitSoFar with any new jobs the current worker can do
// Because workers are sorted, jobIndex and maxProfitSoFar only increase
while (jobIndex < jobs.size() && w >= jobs[jobIndex].first) {
maxProfitSoFar = max(maxProfitSoFar, jobs[jobIndex].second);
jobIndex++;
}
// Add the best possible profit for this worker to the total profit
totalProfit += maxProfitSoFar;
}
return totalProfit;
}
};
Most Profit Assigning Work in TypeScript
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
const n = difficulty.length;
// Create an array of job objects {diff, prof} to associate them
const jobs: { diff: number; prof: number }[] = [];
for (let i = 0; i < n; i++) {
jobs.push({ diff: difficulty[i], prof: profit[i] });
}
// Sort jobs by difficulty in ascending order
jobs.sort((a, b) => a.diff - b.diff);
// Sort workers by ability in ascending order
worker.sort((a, b) => a - b);
let totalProfit = 0;
let maxProfitSoFar = 0;
let jobIndex = 0;
// Iterate through each worker, sorted by ability
for (const w of worker) {
// Update maxProfitSoFar with any new jobs the current worker can do
// Because workers are sorted, jobIndex and maxProfitSoFar only increase
while (jobIndex < n && w >= jobs[jobIndex].diff) {
maxProfitSoFar = Math.max(maxProfitSoFar, jobs[jobIndex].prof);
jobIndex++;
}
// Add the best possible profit for this worker to the total profit
totalProfit += maxProfitSoFar;
}
return totalProfit;
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.