Skip to content

Commit 15fbf1f

Browse files
committedJul 2, 2022
feat: add solutions to lc problems: No.2187,2323
* No.2187.Minimum Time to Complete Trips * No.2323.Find Minimum Time to Finish All Jobs II
1 parent 67ccbe6 commit 15fbf1f

File tree

17 files changed

+534
-29
lines changed

17 files changed

+534
-29
lines changed
 

‎solution/0200-0299/0206.Reverse Linked List/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
**方法二:递归**
5858

59-
递归反转链表的第二个节点到尾部的所有节点,然后 head 插在反转后的链表的尾部。
59+
递归反转链表的第二个节点到尾部的所有节点,然后 $head$ 插在反转后的链表的尾部。
6060

6161
<!-- tabs:start -->
6262

‎solution/2100-2199/2187.Minimum Time to Complete Trips/README.md

+67-1
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,81 @@
5757
<!-- 这里可写当前语言的特殊实现逻辑 -->
5858

5959
```python
60-
60+
class Solution:
61+
def minimumTime(self, time: List[int], totalTrips: int) -> int:
62+
mx = min(time) * totalTrips
63+
return bisect_left(range(mx), totalTrips, key=lambda x: sum(x // v for v in time))
6164
```
6265

6366
### **Java**
6467

6568
<!-- 这里可写当前语言的特殊实现逻辑 -->
6669

6770
```java
71+
class Solution {
72+
public long minimumTime(int[] time, int totalTrips) {
73+
int mi = time[0];
74+
for (int v : time) {
75+
mi = Math.min(mi, v);
76+
}
77+
long left = 1, right = (long) mi * totalTrips;
78+
while (left < right) {
79+
long cnt = 0;
80+
long mid = (left + right) >> 1;
81+
for (int v : time) {
82+
cnt += mid / v;
83+
}
84+
if (cnt >= totalTrips) {
85+
right = mid;
86+
} else {
87+
left = mid + 1;
88+
}
89+
}
90+
return left;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
long long minimumTime(vector<int>& time, int totalTrips) {
101+
int mi = *min_element(time.begin(), time.end());
102+
long long left = 1, right = (long long) mi * totalTrips;
103+
while (left < right)
104+
{
105+
long long cnt = 0;
106+
long long mid = (left + right) >> 1;
107+
for (int v : time) cnt += mid / v;
108+
if (cnt >= totalTrips) right = mid;
109+
else left = mid + 1;
110+
}
111+
return left;
112+
}
113+
};
114+
```
68115
116+
### **Go**
117+
118+
```go
119+
func minimumTime(time []int, totalTrips int) int64 {
120+
left, right := 1, 10000000*totalTrips
121+
for left < right {
122+
mid := (left + right) >> 1
123+
cnt := 0
124+
for _, v := range time {
125+
cnt += mid / v
126+
}
127+
if cnt >= totalTrips {
128+
right = mid
129+
} else {
130+
left = mid + 1
131+
}
132+
}
133+
return int64(left)
134+
}
69135
```
70136

71137
### **TypeScript**

‎solution/2100-2199/2187.Minimum Time to Complete Trips/README_EN.md

+67-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,79 @@ So the minimum time needed to complete 1 trip is 2.
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def minimumTime(self, time: List[int], totalTrips: int) -> int:
56+
mx = min(time) * totalTrips
57+
return bisect_left(range(mx), totalTrips, key=lambda x: sum(x // v for v in time))
5558
```
5659

5760
### **Java**
5861

5962
```java
63+
class Solution {
64+
public long minimumTime(int[] time, int totalTrips) {
65+
int mi = time[0];
66+
for (int v : time) {
67+
mi = Math.min(mi, v);
68+
}
69+
long left = 1, right = (long) mi * totalTrips;
70+
while (left < right) {
71+
long cnt = 0;
72+
long mid = (left + right) >> 1;
73+
for (int v : time) {
74+
cnt += mid / v;
75+
}
76+
if (cnt >= totalTrips) {
77+
right = mid;
78+
} else {
79+
left = mid + 1;
80+
}
81+
}
82+
return left;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
long long minimumTime(vector<int>& time, int totalTrips) {
93+
int mi = *min_element(time.begin(), time.end());
94+
long long left = 1, right = (long long) mi * totalTrips;
95+
while (left < right)
96+
{
97+
long long cnt = 0;
98+
long long mid = (left + right) >> 1;
99+
for (int v : time) cnt += mid / v;
100+
if (cnt >= totalTrips) right = mid;
101+
else left = mid + 1;
102+
}
103+
return left;
104+
}
105+
};
106+
```
60107
108+
### **Go**
109+
110+
```go
111+
func minimumTime(time []int, totalTrips int) int64 {
112+
left, right := 1, 10000000*totalTrips
113+
for left < right {
114+
mid := (left + right) >> 1
115+
cnt := 0
116+
for _, v := range time {
117+
cnt += mid / v
118+
}
119+
if cnt >= totalTrips {
120+
right = mid
121+
} else {
122+
left = mid + 1
123+
}
124+
}
125+
return int64(left)
126+
}
61127
```
62128

63129
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
long long minimumTime(vector<int>& time, int totalTrips) {
4+
int mi = *min_element(time.begin(), time.end());
5+
long long left = 1, right = (long long) mi * totalTrips;
6+
while (left < right)
7+
{
8+
long long cnt = 0;
9+
long long mid = (left + right) >> 1;
10+
for (int v : time) cnt += mid / v;
11+
if (cnt >= totalTrips) right = mid;
12+
else left = mid + 1;
13+
}
14+
return left;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func minimumTime(time []int, totalTrips int) int64 {
2+
left, right := 1, 10000000*totalTrips
3+
for left < right {
4+
mid := (left + right) >> 1
5+
cnt := 0
6+
for _, v := range time {
7+
cnt += mid / v
8+
}
9+
if cnt >= totalTrips {
10+
right = mid
11+
} else {
12+
left = mid + 1
13+
}
14+
}
15+
return int64(left)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public long minimumTime(int[] time, int totalTrips) {
3+
int mi = time[0];
4+
for (int v : time) {
5+
mi = Math.min(mi, v);
6+
}
7+
long left = 1, right = (long) mi * totalTrips;
8+
while (left < right) {
9+
long cnt = 0;
10+
long mid = (left + right) >> 1;
11+
for (int v : time) {
12+
cnt += mid / v;
13+
}
14+
if (cnt >= totalTrips) {
15+
right = mid;
16+
} else {
17+
left = mid + 1;
18+
}
19+
}
20+
return left;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def minimumTime(self, time: List[int], totalTrips: int) -> int:
3+
mx = min(time) * totalTrips
4+
return bisect_left(range(mx), totalTrips, key=lambda x: sum(x // v for v in time))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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>&nbsp;</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>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>n == jobs.length == workers.length</code></li>
48+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
49+
<li><code>1 &lt;= jobs[i], workers[i] &lt;= 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

Comments
 (0)