Problem Description
Given two 0-indexed integer arrays jobs and workers of equal length, where jobs[i] is the amount of work required for the ith job and workers[j] denotes the amount of work a worker can do in one day, you need to assign each job to exactly one worker (and each worker must do exactly one job). When a worker j is assigned a job i, they take ceil(jobs[i] / workers[j]) days to complete it. All workers work concurrently, so the total time to finish all jobs is governed by the longest individual completion time. The goal is to determine the minimum number of days required such that there exists an assignment of jobs to workers where each can finish their assigned job within that number of days.
Key Insights
- For a candidate number of days D, a worker with capacity w can finish any job with work requirement up to w * D.
- Sort both the jobs and workers arrays; then, using a greedy assignment (two pointers), match the smallest available job to any worker who can complete it within D days.
- Use binary search on D (ranging appropriately) to find the minimum valid D that allows all jobs to be assigned satisfying w * D >= job requirement.
- The problem reduces to feasibility checking for a candidate D rather than directly finding an optimal assignment.
Space and Time Complexity
Time Complexity: O(n log M), where n is the length of the arrays and M is the maximum possible number of days (determined by the largest job and the smallest worker); the log M factor comes from the binary search. Space Complexity: O(n), primarily for sorting the arrays (if sorting in-place, the extra space might be O(1) or O(n) depending on the sort algorithm).
Solution
The solution uses a binary search to identify the smallest number of days D for which a valid assignment exists. For each candidate D, sort both jobs and workers in ascending order. Use a two-pointer technique to greedily assign each job to the first worker capable of completing it within D days (i.e. where worker capacity * D is at least the job requirement). If all jobs can be assigned under these conditions, then D is feasible. Adjust the binary search bounds accordingly until the minimum feasible D is found.