[LeetCode] 2323. Find Minimum Time to Finish All Jobs II

You are given two 0-indexed integer arrays jobs and workers of equal length, where jobs[i] is the amount of time needed to complete the ith job, and workers[j] is the amount of time the jth worker can work each day.

Each job should be assigned to exactly one worker, such that each worker completes exactly one job.

Return the minimum number of days needed to complete all the jobs after assignment.

Example 1:

Input: jobs = [5,2,4], workers = [1,7,5]
Output: 2
Explanation:
- Assign the 2nd worker to the 0th job. It takes them 1 day to finish the job.
- Assign the 0th worker to the 1st job. It takes them 2 days to finish the job.
- Assign the 1st worker to the 2nd job. It takes them 1 day to finish the job.
It takes 2 days for all the jobs to be completed, so return 2.
It can be proven that 2 days is the minimum number of days needed.

Example 2:

Input: jobs = [3,18,15,9], workers = [6,5,1,3]
Output: 3
Explanation:
- Assign the 2nd worker to the 0th job. It takes them 3 days to finish the job.
- Assign the 0th worker to the 1st job. It takes them 3 days to finish the job.
- Assign the 1st worker to the 2nd job. It takes them 3 days to finish the job.
- Assign the 3rd worker to the 3rd job. It takes them 3 days to finish the job.
It takes 3 days for all the jobs to be completed, so return 3.
It can be proven that 3 days is the minimum number of days needed.

Constraints:

  • n == jobs.length == workers.length
  • 1 <= n <= 105
  • 1 <= jobs[i], workers[i] <= 105

完成所有工作的最短时间 II。

给你两个 下标从 0 开始 的整数数组 jobs 和 相等 长度的 workers ,其中 jobs[i]是完成第 i 个工作所需的时间,workers[j] 是第 j 个工人每天可以工作的时间。

每项工作都应该 正好 分配给一个工人,这样每个工人就 只能 完成一项工作。

返回分配后完成所有作业所需的最少天数。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-minimum-time-to-finish-all-jobs-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目给了两个长度相同的数组,jobs 和 workers,分别代表每件工作需要多久完成和每个工人每天的工作时间。请你计算完成所有工作最少需要几天。

思路是贪心。把两个 input 数组排序,这样完成工时最短的工作会被工作时间最少的工人完成,完成工时最长的工作会被工作时间最多的工人完成。注意会有类似 job = 1,worker = 2 的 case。对于这种 case,我们需要将整型改成 double 并用 Math.ceil() 函数,确保最后的结果进一。

时间O(nlogn)

空间O(1)

Java实现

 1 class Solution {
 2     public int minimumTime(int[] jobs, int[] workers) {
 3         Arrays.sort(jobs);
 4         Arrays.sort(workers);
 5         int n = jobs.length;
 6         double max = 0;
 7         for (int i = 0; i < n; i++) {
 8             max = Math.max(max, Math.ceil((double) jobs[i] / workers[i]));
 9         }
10         return (int) max;
11     }
12 }

 

LeetCode 题目总结

posted @ 2023-07-20 07:49  CNoodle  阅读(58)  评论(0编辑  收藏  举报