Find Minimum Time to Finish All Jobs - Problem

Imagine you're a project manager at a tech company with k developers on your team. You have a list of jobs where each jobs[i] represents the time (in hours) needed to complete the i-th task.

Your challenge is to distribute these jobs among your developers such that no single developer is overloaded. Each job must be assigned to exactly one developer, and a developer's workload is the sum of all job times assigned to them.

Goal: Find the optimal job assignment that minimizes the maximum workload of any developer. This ensures fair work distribution and project completion in minimum time.

Input: An array jobs of positive integers and an integer k (number of workers)
Output: The minimum possible maximum working time across all workers

Input & Output

example_1.py โ€” Basic Assignment
$ Input: jobs = [3,2,3], k = 3
โ€บ Output: 3
๐Ÿ’ก Note: With 3 workers and 3 jobs, each worker gets exactly one job. The maximum workload is max(3,2,3) = 3.
example_2.py โ€” Optimal Distribution
$ Input: jobs = [1,2,4,7,8], k = 2
โ€บ Output: 11
๐Ÿ’ก Note: Optimal assignment: Worker 1 gets [1,2,8] = 11, Worker 2 gets [4,7] = 11. Both workers have the same workload of 11.
example_3.py โ€” Single Worker
$ Input: jobs = [5,5,4,4,4], k = 1
โ€บ Output: 22
๐Ÿ’ก Note: With only one worker, they must complete all jobs: 5+5+4+4+4 = 22.

Visualization

Tap to expand
Job Distribution VisualizationOriginal Jobs:323Sorted Jobs:332Binary Search Process:min=3try=5max=8Worker Assignment (max=5):Worker 1:Job 3 (3)Worker 2:Job 3+2 (5)Backtracking Tree:RootW1W2Pruning Strategies:Skip workers with same current workloadEarly termination if job exceeds limitBreak if empty worker can't take jobโœ“ Optimal Solution Found: Maximum workload = 5Binary search + backtracking with pruning efficiently finds the minimum possible maximum workload
Understanding the Visualization
1
Sort Jobs by Size
Prioritize larger jobs for better distribution decisions
2
Binary Search Range
Search between max single job and sum of all jobs
3
Feasibility Check
Use backtracking to verify if workload limit is achievable
4
Pruning Optimization
Skip redundant assignments and impossible branches
Key Takeaway
๐ŸŽฏ Key Insight: Binary search on the answer combined with backtracking verification allows us to efficiently explore the solution space while smart pruning techniques dramatically reduce the number of branches explored.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k^n)

Each job can be assigned to any of k workers, leading to k^n total assignments

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space needed to store current assignment and worker workloads

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค k โ‰ค jobs.length โ‰ค 12
  • 1 โ‰ค jobs[i] โ‰ค 107
  • Small input size allows for backtracking solutions
Asked in
Google 34 Amazon 28 Microsoft 22 Meta 18
42.4K Views
Medium Frequency
~25 min Avg. Time
1.5K 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