Find Minimum Time to Finish All Jobs II - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: jobs = [3,2,3], workers = [1,1,1]
Output: 3
💡 Note: Each worker can work 1 hour per day. Jobs take 3, 2, and 3 hours respectively. The workers need ceil(3/1)=3, ceil(2/1)=2, and ceil(3/1)=3 days. Maximum is 3 days.
Example 2 — Optimal Assignment
$ Input: jobs = [1,2,4,7,8], workers = [2,4,1,4,7]
Output: 2
💡 Note: Optimal assignment: job 8→worker 7 (2 days), job 7→worker 4 (2 days), job 4→worker 4 (1 day), job 2→worker 2 (1 day), job 1→worker 1 (1 day). Maximum is 2 days.
Example 3 — Equal Capacity
$ Input: jobs = [5,5,5], workers = [5,5,5]
Output: 1
💡 Note: Each worker can complete their assigned job in exactly 1 day since job time equals worker capacity.

Constraints

  • 1 ≤ jobs.length == workers.length ≤ 12
  • 1 ≤ jobs[i], workers[i] ≤ 1000

Visualization

Tap to expand
Find Minimum Time to Finish All Jobs II INPUT Jobs Array 3 2 3 [0] [1] [2] Workers Array 1 1 1 [0] [1] [2] Input Values: jobs = [3, 2, 3] workers = [1, 1, 1] Each worker works 1 hr/day ALGORITHM STEPS 1 Sort Both Arrays jobs: [2,3,3] workers: [1,1,1] 2 Match by Index Pair jobs[i] with workers[i] 3 Calculate Days days = ceil(job / worker) Job 2 / Worker 1 = 2 days Job 3 / Worker 1 = 3 days Job 3 / Worker 1 = 3 days Results: [2, 3, 3] 4 Find Maximum max(2, 3, 3) = 3 FINAL RESULT Optimal Assignment Job 2 Worker 1 2 days Job 3 Worker 1 3 days Job 3 Worker 1 3 days Output 3 All jobs complete in 3 days minimum - OK Key Insight: Greedy approach: Sort both arrays and pair them by index. The largest job goes to the fastest worker, ensuring optimal assignment. The answer is max(ceil(jobs[i]/workers[i])) since all workers work in parallel. TutorialsPoint - Find Minimum Time to Finish All Jobs II | Greedy - Sort and Match Optimally
Asked in
Google 25 Amazon 18 Microsoft 15 Facebook 12
12.5K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen