You have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where:
difficulty[i] and profit[i] are the difficulty and the profit of the ith job, andworker[j] is the ability of jth worker (i.e., the jth worker can only complete a job with difficulty at most worker[j]).Every worker can be assigned at most one job, but one job can be completed multiple times.
$1, then the total profit will be $3. If a worker cannot complete any job, their profit is $0.Return the maximum profit we can achieve after assigning the workers to the jobs.
Example 1:
Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get a profit of [20,20,30,30] separately.
Example 2:
Input: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25] Output: 0
Constraints:
n == difficulty.lengthn == profit.lengthm == worker.length1 <= n, m <= 1041 <= difficulty[i], profit[i], worker[i] <= 105In this approach, we sort both jobs by difficulty and workers by their ability. We then iterate over each worker and find the highest profit job they can perform using a greedy approach.
First, we pair each job's difficulty with its profit and then sort these pairs by difficulty. We also sort the worker array. Next, for each worker, we iterate through the sorted job list and keep track of the maximum profit the worker can obtain, given that their ability must be greater than or equal to the job's difficulty. This ensures that each worker is assigned the most profitable job they can perform, thus maximizing the total profit.
The solution first sorts the jobs based on their difficulty and profits, and then sorts the workers based on their ability. It uses a while loop to traverse through eligible jobs for each worker and tracks the maximum profit they can make, which is then added to the total profit.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n + m log m)
Space Complexity: O(n)
This approach improves efficiency by preparing the job list in advance for profit maximization, and processes each worker in one pass. The basic idea is to preprocess the jobs to track the maximum profit obtainable up to each difficulty level. We create a running maximum profit and apply this to each worker based on their ability directly.
First, jobs are paired and sorted by difficulty; then, as we iterate through them, we constantly update the maximum profit obtainable up to each job's difficulty. When assessing workers, we simply apply their ability to this precomputed list to find the applicable maximum profit, ensuring minimal lookups and passing through the sorted jobs just once.
This C implementation sorts jobs and workers, and computes a running maximum profit for each difficulty level. The logic uses these precomputed profits to quickly sum the maximum applicable profits for each worker in a single iteration.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n + m log m)
Space Complexity: O(n)
| Approach | Complexity |
|---|---|
| Approach 1: Sorting and Greedy Matching | Time Complexity: O(n log n + m log m) |
| Approach 2: Efficient Profit Calculation with One Pass | Time Complexity: O(n log n + m log m) |
Most Profit Assigning Work | Multiple Approaches | With Intuition | Leetcode 826 | codestorywithMIK • codestorywithMIK • 8,959 views views
Watch 9 more video solutions →Practice Most Profit Assigning Work with our built-in code editor and test cases.
Practice on FleetCode