#1723
Find Minimum Time to Finish All Jobs
HardArrayDynamic ProgrammingBacktrackingBit ManipulationBitmaskBacktrackingBit Manipulation
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n!) | O(k * 2^n) |
| Space | O(n) | O(k) |
💡
Intuition
Time O(k * 2^n)Space O(k)
The optimal solution uses a backtracking approach combined with bit manipulation to efficiently explore job assignments. This reduces the number of combinations we need to check, allowing us to find the minimum maximum working time more quickly.
⚙️
Algorithm
3 steps- 1Step 1: Sort the jobs in descending order to prioritize larger jobs first.
- 2Step 2: Use a backtracking function to assign jobs to workers, tracking the current maximum working time.
- 3Step 3: If the current maximum exceeds the best found so far, backtrack. If all jobs are assigned, update the best maximum if it's lower.
solution.py18 lines
1def minimumTimeRequired(jobs, k):
2 jobs.sort(reverse=True)
3 workers = [0] * k
4 best = float('inf')
5
6 def backtrack(i):
7 nonlocal best
8 if i == len(jobs):
9 best = min(best, max(workers))
10 return
11 for j in range(k):
12 workers[j] += jobs[i]
13 if workers[j] < best:
14 backtrack(i + 1)
15 workers[j] -= jobs[i]
16
17 backtrack(0)
18 return bestℹ
Complexity note: This complexity arises from the backtracking approach, where we explore combinations of job assignments while keeping track of the maximum time for each worker.
- 1Sorting jobs helps prioritize larger tasks, minimizing the maximum time.
- 2Backtracking allows for efficient exploration of job assignments.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.