forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind Minimum Time to Finish All Jobs.py
37 lines (28 loc) · 1.19 KB
/
Find Minimum Time to Finish All Jobs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution:
def dfs(self, pos: int, jobs: List[int], workers: List[int]) -> int:
if pos >= len(jobs):
return max(workers)
mn = float("inf")
# we keep track of visited here to skip workers
# with the same current value of assigned work
# this is an important step in pruning the number
# of workers to explore
visited = set()
for widx in range(len(workers)):
if workers[widx] in visited:
continue
visited.add(workers[widx])
# try this worker
workers[widx] += jobs[pos]
if max(workers) < mn:
# if it's better than our previous proceed
res = self.dfs(pos+1, jobs, workers)
mn = min(mn, res)
# backtrack
workers[widx] -= jobs[pos]
return mn
def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
# sorting the jobs means that highest value jobs are assigned first
# and more computations can be skipped by pruning
jobs.sort(reverse=True)
return self.dfs(0, jobs, [0] * k)