Find Minimum Time to Finish All Jobs II - Problem

You are given two 0-indexed integer arrays jobs and workers of equal length, representing a perfect job assignment scenario.

The Problem: You need to assign each job to exactly one worker such that:

  • jobs[i] represents the time needed to complete the i-th job
  • workers[j] represents the daily working capacity of the j-th worker
  • Each job must be assigned to exactly one worker
  • Each worker gets exactly one job

Goal: Find the minimum number of days needed to complete all jobs after optimal assignment.

Example: If we have jobs [3, 2, 3] and workers [1, 2, 3], the optimal assignment pairs the longest job with the most capable worker to minimize the maximum completion time.

Input & Output

example_1.py โ€” Basic Assignment
$ Input: jobs = [3, 2, 3], workers = [1, 2, 3]
โ€บ Output: 2
๐Ÿ’ก Note: Optimal assignment: Job[3,3,2] โ†’ Worker[3,2,1] gives max(1,2,2) = 2 days
example_2.py โ€” Equal Capacity
$ Input: jobs = [1, 1], workers = [1, 1]
โ€บ Output: 1
๐Ÿ’ก Note: All jobs take 1 day regardless of assignment, so minimum is 1 day
example_3.py โ€” Large Job Difference
$ Input: jobs = [5, 1], workers = [1, 5]
โ€บ Output: 1
๐Ÿ’ก Note: Assign job 5 to worker 5 (1 day) and job 1 to worker 1 (1 day) = 1 day total

Visualization

Tap to expand
๐Ÿณ Restaurant Kitchen AssignmentDishes to Prepare:ComplexDish (3h)SimpleDish (2h)ComplexDish (3h)Available Chefs:ExpertChef (3x)GoodChef (2x)SlowChef (1x)๐ŸŽฏ Optimal Assignment StrategyComplex Dish (3h) โ†’ Expert Chef (3x) = 1 hourComplex Dish (3h) โ†’ Good Chef (2x) = 1.5 hoursSimple Dish (2h) โ†’ Slow Chef (1x) = 2 hoursBottleneck: 2 hours (slowest chef)All dishes ready in 2 hours!๐Ÿ’ก Why This WorksBy pairing hardest tasks with most capable workers, we minimize the bottleneck and reduce total completion time!
Understanding the Visualization
1
List All Orders
We have dishes of varying complexity and chefs with different skill levels
2
Strategic Assignment
Assign the most complex dishes to the most skilled chefs
3
Minimize Wait Time
The slowest chef determines when all dishes are ready
Key Takeaway
๐ŸŽฏ Key Insight: The greedy strategy of pairing the most demanding jobs with the most capable workers minimizes the bottleneck, ensuring optimal completion time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n! ร— n)

n! permutations, each taking O(n) time to evaluate

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for storing current permutation and tracking minimum

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค jobs.length โ‰ค 1000
  • jobs.length == workers.length
  • 1 โ‰ค jobs[i], workers[i] โ‰ค 104
  • Each job must be assigned to exactly one worker
  • Each worker gets exactly one job
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K 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