diff --git a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README.md b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README.md index 81a63e74d5406..c4e8230ca9808 100644 --- a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README.md +++ b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README.md @@ -69,7 +69,13 @@ tags: -### 方法一 +### 方法一:贪心 + +为了使得完成所有工作所需的最少天数尽可能小,我们可以尽量让工作时间较长的工人去完成工作时间较长的工作。 + +因此,我们可以先对 $\textit{jobs}$ 和 $\textit{workers}$ 进行排序,然后依次将工作分配给对应下标的工人,求最大的工作时间和工人时间的比值即可。 + +时间复杂度 $O(n \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为工作数。 @@ -105,10 +111,13 @@ class Solution { class Solution { public: int minimumTime(vector& jobs, vector& workers) { - sort(jobs.begin(), jobs.end()); - sort(workers.begin(), workers.end()); + ranges::sort(jobs); + ranges::sort(workers); int ans = 0; - for (int i = 0; i < jobs.size(); ++i) ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]); + int n = jobs.size(); + for (int i = 0; i < n; ++i) { + ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]); + } return ans; } }; @@ -117,18 +126,68 @@ public: #### Go ```go -func minimumTime(jobs []int, workers []int) int { +func minimumTime(jobs []int, workers []int) (ans int) { sort.Ints(jobs) sort.Ints(workers) - ans := 0 for i, a := range jobs { b := workers[i] ans = max(ans, (a+b-1)/b) } - return ans + return +} +``` + +#### TypeScript + +```ts +function minimumTime(jobs: number[], workers: number[]): number { + jobs.sort((a, b) => a - b); + workers.sort((a, b) => a - b); + let ans = 0; + const n = jobs.length; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, Math.ceil(jobs[i] / workers[i])); + } + return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_time(mut jobs: Vec, mut workers: Vec) -> i32 { + jobs.sort(); + workers.sort(); + jobs.iter() + .zip(workers.iter()) + .map(|(a, b)| (a + b - 1) / b) + .max() + .unwrap() + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} jobs + * @param {number[]} workers + * @return {number} + */ +var minimumTime = function (jobs, workers) { + jobs.sort((a, b) => a - b); + workers.sort((a, b) => a - b); + let ans = 0; + const n = jobs.length; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, Math.ceil(jobs[i] / workers[i])); + } + return ans; +}; +``` + diff --git a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README_EN.md b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README_EN.md index ffc9b3761674f..4787d762cdf9c 100644 --- a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README_EN.md +++ b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README_EN.md @@ -67,7 +67,13 @@ It can be proven that 3 days is the minimum number of days needed. -### Solution 1 +### Solution 1: Greedy + +To minimize the number of days required to complete all jobs, we can try to assign longer jobs to workers who can work longer hours. + +Therefore, we can first sort $\textit{jobs}$ and $\textit{workers}$, then assign jobs to workers based on their indices. Finally, we calculate the maximum ratio of job time to worker time. + +The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of jobs. @@ -103,10 +109,13 @@ class Solution { class Solution { public: int minimumTime(vector& jobs, vector& workers) { - sort(jobs.begin(), jobs.end()); - sort(workers.begin(), workers.end()); + ranges::sort(jobs); + ranges::sort(workers); int ans = 0; - for (int i = 0; i < jobs.size(); ++i) ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]); + int n = jobs.size(); + for (int i = 0; i < n; ++i) { + ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]); + } return ans; } }; @@ -115,18 +124,68 @@ public: #### Go ```go -func minimumTime(jobs []int, workers []int) int { +func minimumTime(jobs []int, workers []int) (ans int) { sort.Ints(jobs) sort.Ints(workers) - ans := 0 for i, a := range jobs { b := workers[i] ans = max(ans, (a+b-1)/b) } - return ans + return +} +``` + +#### TypeScript + +```ts +function minimumTime(jobs: number[], workers: number[]): number { + jobs.sort((a, b) => a - b); + workers.sort((a, b) => a - b); + let ans = 0; + const n = jobs.length; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, Math.ceil(jobs[i] / workers[i])); + } + return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_time(mut jobs: Vec, mut workers: Vec) -> i32 { + jobs.sort(); + workers.sort(); + jobs.iter() + .zip(workers.iter()) + .map(|(a, b)| (a + b - 1) / b) + .max() + .unwrap() + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} jobs + * @param {number[]} workers + * @return {number} + */ +var minimumTime = function (jobs, workers) { + jobs.sort((a, b) => a - b); + workers.sort((a, b) => a - b); + let ans = 0; + const n = jobs.length; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, Math.ceil(jobs[i] / workers[i])); + } + return ans; +}; +``` + diff --git a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.cpp b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.cpp index 64157068e5b04..4c2649f0aed4e 100644 --- a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.cpp +++ b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.cpp @@ -1,10 +1,13 @@ class Solution { public: int minimumTime(vector& jobs, vector& workers) { - sort(jobs.begin(), jobs.end()); - sort(workers.begin(), workers.end()); + ranges::sort(jobs); + ranges::sort(workers); int ans = 0; - for (int i = 0; i < jobs.size(); ++i) ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]); + int n = jobs.size(); + for (int i = 0; i < n; ++i) { + ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]); + } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.go b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.go index 1c2f83f16ed1b..6ded2acab2c80 100644 --- a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.go +++ b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.go @@ -1,10 +1,9 @@ -func minimumTime(jobs []int, workers []int) int { +func minimumTime(jobs []int, workers []int) (ans int) { sort.Ints(jobs) sort.Ints(workers) - ans := 0 for i, a := range jobs { b := workers[i] ans = max(ans, (a+b-1)/b) } - return ans -} \ No newline at end of file + return +} diff --git a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.js b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.js new file mode 100644 index 0000000000000..72f735d55b1a8 --- /dev/null +++ b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} jobs + * @param {number[]} workers + * @return {number} + */ +var minimumTime = function (jobs, workers) { + jobs.sort((a, b) => a - b); + workers.sort((a, b) => a - b); + let ans = 0; + const n = jobs.length; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, Math.ceil(jobs[i] / workers[i])); + } + return ans; +}; diff --git a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.rs b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.rs new file mode 100644 index 0000000000000..0db2fc5731559 --- /dev/null +++ b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.rs @@ -0,0 +1,11 @@ +impl Solution { + pub fn minimum_time(mut jobs: Vec, mut workers: Vec) -> i32 { + jobs.sort(); + workers.sort(); + jobs.iter() + .zip(workers.iter()) + .map(|(a, b)| (a + b - 1) / b) + .max() + .unwrap() + } +} diff --git a/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.ts b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.ts new file mode 100644 index 0000000000000..82c687f61cf0c --- /dev/null +++ b/solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/Solution.ts @@ -0,0 +1,10 @@ +function minimumTime(jobs: number[], workers: number[]): number { + jobs.sort((a, b) => a - b); + workers.sort((a, b) => a - b); + let ans = 0; + const n = jobs.length; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, Math.ceil(jobs[i] / workers[i])); + } + return ans; +}