Skip to content

Commit f5cfd10

Browse files
authored
Create 1723-find-minimum-time-to-finish-all-jobs.js
1 parent 4802f4d commit f5cfd10

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {number[]} jobs
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const minimumTimeRequired = function (jobs, k) {
7+
if (jobs.length <= k) {
8+
return Math.max(...jobs)
9+
}
10+
11+
// create a store to hold the number of hours each worker worked
12+
const workers = new Array(k).fill(0)
13+
14+
let minLongestWorkingTime = Infinity
15+
const dfs = (i) => {
16+
if (i === jobs.length) {
17+
// if we assigned all the jobs, see if we have a better result
18+
minLongestWorkingTime = Math.min(
19+
minLongestWorkingTime,
20+
Math.max(...workers)
21+
)
22+
return
23+
}
24+
const lengthOfWork = jobs[i]
25+
26+
for (let worker = 0; worker < k; worker++) {
27+
workers[worker] += lengthOfWork
28+
29+
// if this combination is has a chance of decreasing our
30+
// answer, try it, otherwise skip it to save on time.
31+
if (workers[worker] <= minLongestWorkingTime) {
32+
dfs(i + 1)
33+
}
34+
workers[worker] -= lengthOfWork
35+
36+
// We want to minimize the width of the tree
37+
// so if the worker has gotten their first job
38+
// don't try any workers after this worker.
39+
// All other workers after this worker will be 0 as well
40+
// so the combination is exactly the same.
41+
if (workers[worker] === 0) break
42+
}
43+
}
44+
45+
dfs(0)
46+
return minLongestWorkingTime
47+
}

0 commit comments

Comments
 (0)