|
| 1 | +# [2323. Find Minimum Time to Finish All Jobs II](https://leetcode.cn/problems/find-minimum-time-to-finish-all-jobs-ii) |
| 2 | + |
| 3 | +[English Version](/solution/2300-2399/2323.Find%20Minimum%20Time%20to%20Finish%20All%20Jobs%20II/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given two <strong>0-indexed</strong> integer arrays <code>jobs</code> and <code>workers</code> of <strong>equal</strong> length, where <code>jobs[i]</code> is the amount of time needed to complete the <code>i<sup>th</sup></code> job, and <code>workers[j]</code> is the amount of time the <code>j<sup>th</sup></code> worker can work each day.</p> |
| 10 | + |
| 11 | +<p>Each job should be assigned to <strong>exactly</strong> one worker, such that each worker completes <strong>exactly</strong> one job.</p> |
| 12 | + |
| 13 | +<p>Return <em>the <strong>minimum</strong> number of days needed to complete all the jobs after assignment.</em></p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong>Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> jobs = [5,2,4], workers = [1,7,5] |
| 20 | +<strong>Output:</strong> 2 |
| 21 | +<strong>Explanation:</strong> |
| 22 | +- Assign the 2<sup>nd</sup> worker to the 0<sup>th</sup> job. It takes them 1 day to finish the job. |
| 23 | +- Assign the 0<sup>th</sup> worker to the 1<sup>st</sup> job. It takes them 2 days to finish the job. |
| 24 | +- Assign the 1<sup>st</sup> worker to the 2<sup>nd</sup> job. It takes them 1 day to finish the job. |
| 25 | +It takes 2 days for all the jobs to be completed, so return 2. |
| 26 | +It can be proven that 2 days is the minimum number of days needed. |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p><strong>Example 2:</strong></p> |
| 30 | + |
| 31 | +<pre> |
| 32 | +<strong>Input:</strong> jobs = [3,18,15,9], workers = [6,5,1,3] |
| 33 | +<strong>Output:</strong> 3 |
| 34 | +<strong>Explanation:</strong> |
| 35 | +- Assign the 2<sup>nd</sup> worker to the 0<sup>th</sup> job. It takes them 3 days to finish the job. |
| 36 | +- Assign the 0<sup>th</sup> worker to the 1<sup>st</sup> job. It takes them 3 days to finish the job. |
| 37 | +- Assign the 1<sup>st</sup> worker to the 2<sup>nd</sup> job. It takes them 3 days to finish the job. |
| 38 | +- Assign the 3<sup>rd</sup> worker to the 3<sup>rd</sup> job. It takes them 3 days to finish the job. |
| 39 | +It takes 3 days for all the jobs to be completed, so return 3. |
| 40 | +It can be proven that 3 days is the minimum number of days needed. |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>n == jobs.length == workers.length</code></li> |
| 48 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 49 | + <li><code>1 <= jobs[i], workers[i] <= 10<sup>5</sup></code></li> |
| 50 | +</ul> |
| 51 | + |
| 52 | + |
| 53 | +## 解法 |
| 54 | + |
| 55 | +<!-- 这里可写通用的实现逻辑 --> |
| 56 | + |
| 57 | +<!-- tabs:start --> |
| 58 | + |
| 59 | +### **Python3** |
| 60 | + |
| 61 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 62 | + |
| 63 | +```python |
| 64 | +class Solution: |
| 65 | + def minimumTime(self, jobs: List[int], workers: List[int]) -> int: |
| 66 | + jobs.sort() |
| 67 | + workers.sort() |
| 68 | + return max((a + b - 1) // b for a, b in zip(jobs, workers)) |
| 69 | +``` |
| 70 | + |
| 71 | +### **Java** |
| 72 | + |
| 73 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 74 | + |
| 75 | +```java |
| 76 | +class Solution { |
| 77 | + public int minimumTime(int[] jobs, int[] workers) { |
| 78 | + Arrays.sort(jobs); |
| 79 | + Arrays.sort(workers); |
| 80 | + int ans = 0; |
| 81 | + for (int i = 0; i < jobs.length; ++i) { |
| 82 | + ans = Math.max(ans, (jobs[i] + workers[i] - 1) / workers[i]); |
| 83 | + } |
| 84 | + return ans; |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### **C++** |
| 90 | + |
| 91 | +```cpp |
| 92 | +class Solution { |
| 93 | +public: |
| 94 | + int minimumTime(vector<int>& jobs, vector<int>& workers) { |
| 95 | + sort(jobs.begin(), jobs.end()); |
| 96 | + sort(workers.begin(), workers.end()); |
| 97 | + int ans = 0; |
| 98 | + for (int i = 0; i < jobs.size(); ++i) ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]); |
| 99 | + return ans; |
| 100 | + } |
| 101 | +}; |
| 102 | +``` |
| 103 | +
|
| 104 | +### **Go** |
| 105 | +
|
| 106 | +```go |
| 107 | +func minimumTime(jobs []int, workers []int) int { |
| 108 | + sort.Ints(jobs) |
| 109 | + sort.Ints(workers) |
| 110 | + ans := 0 |
| 111 | + for i, a := range jobs { |
| 112 | + b := workers[i] |
| 113 | + ans = max(ans, (a+b-1)/b) |
| 114 | + } |
| 115 | + return ans |
| 116 | +} |
| 117 | +
|
| 118 | +func max(a, b int) int { |
| 119 | + if a > b { |
| 120 | + return a |
| 121 | + } |
| 122 | + return b |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### **TypeScript** |
| 127 | + |
| 128 | +```ts |
| 129 | + |
| 130 | +``` |
| 131 | + |
| 132 | +### **...** |
| 133 | + |
| 134 | +``` |
| 135 | +
|
| 136 | +``` |
| 137 | + |
| 138 | +<!-- tabs:end --> |
0 commit comments