Find Minimum Time to Finish All Jobs II Problem
#2323
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 2323.
You are given two 0-indexed integer arrays jobs and workers of equal length, where jobs[i] is the amount of time needed to complete the i^th job, and workers[j] is the amount of time the j^th worker can work each day.
Each job should be assigned to exactly one worker, such that each worker completes exactly one job.
Return the minimum number of days needed to complete all the jobs after assignment.
Example 1:
Input: jobs = [5,2,4], workers = [1,7,5]
Output: 2
Explanation:
- Assign the 2^nd worker to the 0^th job. It takes them 1 day to finish the job.
- Assign the 0^th worker to the 1^st job. It takes them 2 days to finish the job.
- Assign the 1^st worker to the 2^nd job. It takes them 1 day to finish the job.
It takes 2 days for all the jobs to be completed, so return 2.
It can be proven that 2 days is the minimum number of days needed.
Example 2:
Input: jobs = [3,18,15,9], workers = [6,5,1,3]
Output: 3
Explanation:
- Assign the 2^nd worker to the 0^th job. It takes them 3 days to finish the job.
- Assign the 0^th worker to the 1^st job. It takes them 3 days to finish the job.
- Assign the 1^st worker to the 2^nd job. It takes them 3 days to finish the job.
- Assign the 3^rd worker to the 3^rd job. It takes them 3 days to finish the job.
It takes 3 days for all the jobs to be completed, so return 3.
It can be proven that 3 days is the minimum number of days needed.
Constraints:
n == jobs.length == workers.length1 <= n <= 10^51 <= jobs[i], workers[i] <= 10^5
Solution
Approach: Greedy
Pair the longest jobs with the workers who can work the most hours: sort both arrays and match them by index. Each pair needs ceil(job / worker) days, and since everything runs in parallel the answer is the maximum over the pairs.
Complexity: Time O(n log n), space O(1) besides sorting.
class Solution:
def minimumTime(self, jobs: List[int], workers: List[int]) -> int:
jobs.sort()
workers.sort()
return max((a + b - 1) // b for a, b in zip(jobs, workers))