Skip to content

Commit 2fa1a47

Browse files
authored
feat: add solutions to lc problem: No.2323 (#3995)
No.2323.Find Minimum Time to Finish All Jobs II
1 parent d7dfeb5 commit 2fa1a47

File tree

7 files changed

+178
-22
lines changed

7 files changed

+178
-22
lines changed

solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README.md

+66-7
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ tags:
6969

7070
<!-- solution:start -->
7171

72-
### 方法一
72+
### 方法一:贪心
73+
74+
为了使得完成所有工作所需的最少天数尽可能小,我们可以尽量让工作时间较长的工人去完成工作时间较长的工作。
75+
76+
因此,我们可以先对 $\textit{jobs}$ 和 $\textit{workers}$ 进行排序,然后依次将工作分配给对应下标的工人,求最大的工作时间和工人时间的比值即可。
77+
78+
时间复杂度 $O(n \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为工作数。
7379

7480
<!-- tabs:start -->
7581

@@ -105,10 +111,13 @@ class Solution {
105111
class Solution {
106112
public:
107113
int minimumTime(vector<int>& jobs, vector<int>& workers) {
108-
sort(jobs.begin(), jobs.end());
109-
sort(workers.begin(), workers.end());
114+
ranges::sort(jobs);
115+
ranges::sort(workers);
110116
int ans = 0;
111-
for (int i = 0; i < jobs.size(); ++i) ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]);
117+
int n = jobs.size();
118+
for (int i = 0; i < n; ++i) {
119+
ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]);
120+
}
112121
return ans;
113122
}
114123
};
@@ -117,18 +126,68 @@ public:
117126
#### Go
118127
119128
```go
120-
func minimumTime(jobs []int, workers []int) int {
129+
func minimumTime(jobs []int, workers []int) (ans int) {
121130
sort.Ints(jobs)
122131
sort.Ints(workers)
123-
ans := 0
124132
for i, a := range jobs {
125133
b := workers[i]
126134
ans = max(ans, (a+b-1)/b)
127135
}
128-
return ans
136+
return
137+
}
138+
```
139+
140+
#### TypeScript
141+
142+
```ts
143+
function minimumTime(jobs: number[], workers: number[]): number {
144+
jobs.sort((a, b) => a - b);
145+
workers.sort((a, b) => a - b);
146+
let ans = 0;
147+
const n = jobs.length;
148+
for (let i = 0; i < n; ++i) {
149+
ans = Math.max(ans, Math.ceil(jobs[i] / workers[i]));
150+
}
151+
return ans;
129152
}
130153
```
131154

155+
#### Rust
156+
157+
```rust
158+
impl Solution {
159+
pub fn minimum_time(mut jobs: Vec<i32>, mut workers: Vec<i32>) -> i32 {
160+
jobs.sort();
161+
workers.sort();
162+
jobs.iter()
163+
.zip(workers.iter())
164+
.map(|(a, b)| (a + b - 1) / b)
165+
.max()
166+
.unwrap()
167+
}
168+
}
169+
```
170+
171+
#### JavaScript
172+
173+
```js
174+
/**
175+
* @param {number[]} jobs
176+
* @param {number[]} workers
177+
* @return {number}
178+
*/
179+
var minimumTime = function (jobs, workers) {
180+
jobs.sort((a, b) => a - b);
181+
workers.sort((a, b) => a - b);
182+
let ans = 0;
183+
const n = jobs.length;
184+
for (let i = 0; i < n; ++i) {
185+
ans = Math.max(ans, Math.ceil(jobs[i] / workers[i]));
186+
}
187+
return ans;
188+
};
189+
```
190+
132191
<!-- tabs:end -->
133192

134193
<!-- solution:end -->

solution/2300-2399/2323.Find Minimum Time to Finish All Jobs II/README_EN.md

+66-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ It can be proven that 3 days is the minimum number of days needed.
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Greedy
71+
72+
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.
73+
74+
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.
75+
76+
The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of jobs.
7177

7278
<!-- tabs:start -->
7379

@@ -103,10 +109,13 @@ class Solution {
103109
class Solution {
104110
public:
105111
int minimumTime(vector<int>& jobs, vector<int>& workers) {
106-
sort(jobs.begin(), jobs.end());
107-
sort(workers.begin(), workers.end());
112+
ranges::sort(jobs);
113+
ranges::sort(workers);
108114
int ans = 0;
109-
for (int i = 0; i < jobs.size(); ++i) ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]);
115+
int n = jobs.size();
116+
for (int i = 0; i < n; ++i) {
117+
ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]);
118+
}
110119
return ans;
111120
}
112121
};
@@ -115,18 +124,68 @@ public:
115124
#### Go
116125
117126
```go
118-
func minimumTime(jobs []int, workers []int) int {
127+
func minimumTime(jobs []int, workers []int) (ans int) {
119128
sort.Ints(jobs)
120129
sort.Ints(workers)
121-
ans := 0
122130
for i, a := range jobs {
123131
b := workers[i]
124132
ans = max(ans, (a+b-1)/b)
125133
}
126-
return ans
134+
return
135+
}
136+
```
137+
138+
#### TypeScript
139+
140+
```ts
141+
function minimumTime(jobs: number[], workers: number[]): number {
142+
jobs.sort((a, b) => a - b);
143+
workers.sort((a, b) => a - b);
144+
let ans = 0;
145+
const n = jobs.length;
146+
for (let i = 0; i < n; ++i) {
147+
ans = Math.max(ans, Math.ceil(jobs[i] / workers[i]));
148+
}
149+
return ans;
127150
}
128151
```
129152

153+
#### Rust
154+
155+
```rust
156+
impl Solution {
157+
pub fn minimum_time(mut jobs: Vec<i32>, mut workers: Vec<i32>) -> i32 {
158+
jobs.sort();
159+
workers.sort();
160+
jobs.iter()
161+
.zip(workers.iter())
162+
.map(|(a, b)| (a + b - 1) / b)
163+
.max()
164+
.unwrap()
165+
}
166+
}
167+
```
168+
169+
#### JavaScript
170+
171+
```js
172+
/**
173+
* @param {number[]} jobs
174+
* @param {number[]} workers
175+
* @return {number}
176+
*/
177+
var minimumTime = function (jobs, workers) {
178+
jobs.sort((a, b) => a - b);
179+
workers.sort((a, b) => a - b);
180+
let ans = 0;
181+
const n = jobs.length;
182+
for (let i = 0; i < n; ++i) {
183+
ans = Math.max(ans, Math.ceil(jobs[i] / workers[i]));
184+
}
185+
return ans;
186+
};
187+
```
188+
130189
<!-- tabs:end -->
131190

132191
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
class Solution {
22
public:
33
int minimumTime(vector<int>& jobs, vector<int>& workers) {
4-
sort(jobs.begin(), jobs.end());
5-
sort(workers.begin(), workers.end());
4+
ranges::sort(jobs);
5+
ranges::sort(workers);
66
int ans = 0;
7-
for (int i = 0; i < jobs.size(); ++i) ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]);
7+
int n = jobs.size();
8+
for (int i = 0; i < n; ++i) {
9+
ans = max(ans, (jobs[i] + workers[i] - 1) / workers[i]);
10+
}
811
return ans;
912
}
10-
};
13+
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
func minimumTime(jobs []int, workers []int) int {
1+
func minimumTime(jobs []int, workers []int) (ans int) {
22
sort.Ints(jobs)
33
sort.Ints(workers)
4-
ans := 0
54
for i, a := range jobs {
65
b := workers[i]
76
ans = max(ans, (a+b-1)/b)
87
}
9-
return ans
10-
}
8+
return
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} jobs
3+
* @param {number[]} workers
4+
* @return {number}
5+
*/
6+
var minimumTime = function (jobs, workers) {
7+
jobs.sort((a, b) => a - b);
8+
workers.sort((a, b) => a - b);
9+
let ans = 0;
10+
const n = jobs.length;
11+
for (let i = 0; i < n; ++i) {
12+
ans = Math.max(ans, Math.ceil(jobs[i] / workers[i]));
13+
}
14+
return ans;
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn minimum_time(mut jobs: Vec<i32>, mut workers: Vec<i32>) -> i32 {
3+
jobs.sort();
4+
workers.sort();
5+
jobs.iter()
6+
.zip(workers.iter())
7+
.map(|(a, b)| (a + b - 1) / b)
8+
.max()
9+
.unwrap()
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function minimumTime(jobs: number[], workers: number[]): number {
2+
jobs.sort((a, b) => a - b);
3+
workers.sort((a, b) => a - b);
4+
let ans = 0;
5+
const n = jobs.length;
6+
for (let i = 0; i < n; ++i) {
7+
ans = Math.max(ans, Math.ceil(jobs[i] / workers[i]));
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
 (0)