Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2323 #3995

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心

为了使得完成所有工作所需的最少天数尽可能小,我们可以尽量让工作时间较长的工人去完成工作时间较长的工作。

因此,我们可以先对 $\textit{jobs}$ 和 $\textit{workers}$ 进行排序,然后依次将工作分配给对应下标的工人,求最大的工作时间和工人时间的比值即可。

时间复杂度 $O(n \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为工作数。

<!-- tabs:start -->

Expand Down Expand Up @@ -105,10 +111,13 @@ class Solution {
class Solution {
public:
int minimumTime(vector<int>& jobs, vector<int>& 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;
}
};
Expand All @@ -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<i32>, mut workers: Vec<i32>) -> 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;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ It can be proven that 3 days is the minimum number of days needed.

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -103,10 +109,13 @@ class Solution {
class Solution {
public:
int minimumTime(vector<int>& jobs, vector<int>& 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;
}
};
Expand All @@ -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<i32>, mut workers: Vec<i32>) -> 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;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
class Solution {
public:
int minimumTime(vector<int>& jobs, vector<int>& 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;
}
};
};
Original file line number Diff line number Diff line change
@@ -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
}
return
}
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
impl Solution {
pub fn minimum_time(mut jobs: Vec<i32>, mut workers: Vec<i32>) -> i32 {
jobs.sort();
workers.sort();
jobs.iter()
.zip(workers.iter())
.map(|(a, b)| (a + b - 1) / b)
.max()
.unwrap()
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Loading