Find Minimum Time to Finish All Jobs — LeetCode 1723 Python Solution

HardBit ManipulationArrayDynamic ProgrammingBacktrackingBitmask
Problem
#1723
Reading time
4 min

The problem

You are given an integer array jobs, where jobs[i] is the amount of time it takes to complete the ith job. There are k workers that you can assign jobs to.

Example

Input
jobs = [3,2,3], k = 3
Output
3
Explanation
By assigning each person one job, the maximum time is 3.

Python solution

Python
class Solution:
    def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
        def dfs(i):
            nonlocal ans
            if i == len(jobs):
                ans = min(ans, max(cnt))
                return
            for j in range(k):
                if cnt[j] + jobs[i] >= ans:
                    continue
                cnt[j] += jobs[i]
                dfs(i + 1)
                cnt[j] -= jobs[i]
                if cnt[j] == 0:
                    break

        cnt = [0] * k
        jobs.sort(reverse=True)
        ans = inf
        dfs(0)
        return ans

Complexity

MeasureComplexity
TimeO(n·m) (typical)
SpaceO(n·m) or optimized auxiliary

Pattern: Backtracking

Build candidates one choice at a time and abandon a branch the moment it cannot work. LeetCode 1723. Find Minimum Time to Finish All Jobs is filed here because LeetCode tags it Backtracking, which is the vocabulary this hub collects.

The backtracking guide has the Python template for the pattern and the 105 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1723. Find Minimum Time to Finish All Jobs?
LeetCode 1723. Find Minimum Time to Finish All Jobs is rated Hard on LeetCode.
What topics does LeetCode 1723. Find Minimum Time to Finish All Jobs cover?
LeetCode 1723. Find Minimum Time to Finish All Jobs is tagged Bit Manipulation, Array, Dynamic Programming, Backtracking and Bitmask on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview