Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d974b9e

Browse files
committed
Add solution #1723
1 parent f3763d6 commit d974b9e

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,501 LeetCode solutions in JavaScript
1+
# 1,502 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1328,6 +1328,7 @@
13281328
1720|[Decode XORed Array](./solutions/1720-decode-xored-array.js)|Easy|
13291329
1721|[Swapping Nodes in a Linked List](./solutions/1721-swapping-nodes-in-a-linked-list.js)|Medium|
13301330
1722|[Minimize Hamming Distance After Swap Operations](./solutions/1722-minimize-hamming-distance-after-swap-operations.js)|Medium|
1331+
1723|[Find Minimum Time to Finish All Jobs](./solutions/1723-find-minimum-time-to-finish-all-jobs.js)|Hard|
13311332
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
13321333
1732|[Find the Highest Altitude](./solutions/1732-find-the-highest-altitude.js)|Easy|
13331334
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* 1723. Find Minimum Time to Finish All Jobs
3+
* https://leetcode.com/problems/find-minimum-time-to-finish-all-jobs/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array jobs, where jobs[i] is the amount of time it takes to complete
7+
* the ith job.
8+
*
9+
* There are k workers that you can assign jobs to. Each job should be assigned to exactly one
10+
* worker. The working time of a worker is the sum of the time it takes to complete all jobs
11+
* assigned to them. Your goal is to devise an optimal assignment such that the maximum working
12+
* time of any worker is minimized.
13+
*
14+
* Return the minimum possible maximum working time of any assignment.
15+
*/
16+
17+
/**
18+
* @param {number[]} jobs
19+
* @param {number} k
20+
* @return {number}
21+
*/
22+
var minimumTimeRequired = function(jobs, k) {
23+
const n = jobs.length;
24+
const workerTimes = new Array(k).fill(0);
25+
let result = Infinity;
26+
27+
jobs.sort((a, b) => b - a);
28+
backtrack(0, 0);
29+
30+
return result;
31+
32+
function backtrack(jobIndex, maxTime) {
33+
if (jobIndex === n) {
34+
result = Math.min(result, maxTime);
35+
return;
36+
}
37+
38+
if (maxTime >= result) return;
39+
40+
for (let i = 0; i < k; i++) {
41+
if (workerTimes[i] + jobs[jobIndex] >= result) continue;
42+
43+
workerTimes[i] += jobs[jobIndex];
44+
backtrack(jobIndex + 1, Math.max(maxTime, workerTimes[i]));
45+
workerTimes[i] -= jobs[jobIndex];
46+
47+
if (workerTimes[i] === 0) break;
48+
}
49+
}
50+
};

0 commit comments

Comments
 (0)