Skip to content

Commit 02f5e66

Browse files
authored
feat: add solutions to lc problems: No.3633,3635 (#4615)
1 parent 182fba7 commit 02f5e66

File tree

14 files changed

+592
-16
lines changed

14 files changed

+592
-16
lines changed

solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README.md

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,129 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3633.Ea
128128

129129
<!-- solution:start -->
130130

131-
### 方法一
131+
### 方法一:枚举 + 贪心
132+
133+
我们可以考虑两种游乐设施的顺序,先玩陆地游乐设施再玩水上游乐设施,或者先玩水上游乐设施再玩陆地游乐设施。
134+
135+
对于每种顺序,我们先计算出第一种游乐设施的最早结束时间 $\textit{minEnd}$,然后枚举第二种游乐设施,计算出第二种游乐设施的最早结束时间 $\max(\textit{minEnd}, \textit{startTime}) + \textit{duration}$,其中 $\textit{startTime}$ 是第二种游乐设施的开始时间。我们取所有可能的最早结束时间的最小值作为答案。
136+
137+
最后,我们返回两种顺序的答案中的最小值。
138+
139+
时间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是陆地游乐设施和水上游乐设施的数量。空间复杂度 $O(1)$。
132140

133141
<!-- tabs:start -->
134142

135143
#### Python3
136144

137145
```python
138-
146+
class Solution:
147+
def earliestFinishTime(self, landStartTime: List[int], landDuration: List[int], waterStartTime: List[int], waterDuration: List[int]) -> int:
148+
def calc(a1, t1, a2, t2):
149+
min_end = min(a + t for a, t in zip(a1, t1))
150+
return min(max(a, min_end) + t for a, t in zip(a2, t2))
151+
152+
x = calc(landStartTime, landDuration, waterStartTime, waterDuration)
153+
y = calc(waterStartTime, waterDuration, landStartTime, landDuration)
154+
return min(x, y)
139155
```
140156

141157
#### Java
142158

143159
```java
144-
160+
class Solution {
161+
public int earliestFinishTime(
162+
int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) {
163+
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
164+
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
165+
return Math.min(x, y);
166+
}
167+
168+
private int calc(int[] a1, int[] t1, int[] a2, int[] t2) {
169+
int minEnd = Integer.MAX_VALUE;
170+
for (int i = 0; i < a1.length; ++i) {
171+
minEnd = Math.min(minEnd, a1[i] + t1[i]);
172+
}
173+
int ans = Integer.MAX_VALUE;
174+
for (int i = 0; i < a2.length; ++i) {
175+
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
176+
}
177+
return ans;
178+
}
179+
}
145180
```
146181

147182
#### C++
148183

149184
```cpp
150-
185+
class Solution {
186+
public:
187+
int earliestFinishTime(vector<int>& landStartTime, vector<int>& landDuration, vector<int>& waterStartTime, vector<int>& waterDuration) {
188+
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
189+
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
190+
return min(x, y);
191+
}
192+
193+
int calc(vector<int>& a1, vector<int>& t1, vector<int>& a2, vector<int>& t2) {
194+
int minEnd = INT_MAX;
195+
for (int i = 0; i < a1.size(); ++i) {
196+
minEnd = min(minEnd, a1[i] + t1[i]);
197+
}
198+
int ans = INT_MAX;
199+
for (int i = 0; i < a2.size(); ++i) {
200+
ans = min(ans, max(minEnd, a2[i]) + t2[i]);
201+
}
202+
return ans;
203+
}
204+
};
151205
```
152206

153207
#### Go
154208

155209
```go
210+
func earliestFinishTime(landStartTime []int, landDuration []int, waterStartTime []int, waterDuration []int) int {
211+
x := calc(landStartTime, landDuration, waterStartTime, waterDuration)
212+
y := calc(waterStartTime, waterDuration, landStartTime, landDuration)
213+
return min(x, y)
214+
}
215+
216+
func calc(a1 []int, t1 []int, a2 []int, t2 []int) int {
217+
minEnd := math.MaxInt32
218+
for i := 0; i < len(a1); i++ {
219+
minEnd = min(minEnd, a1[i]+t1[i])
220+
}
221+
ans := math.MaxInt32
222+
for i := 0; i < len(a2); i++ {
223+
ans = min(ans, max(minEnd, a2[i])+t2[i])
224+
}
225+
return ans
226+
}
227+
```
156228

229+
#### TypeScript
230+
231+
```ts
232+
function earliestFinishTime(
233+
landStartTime: number[],
234+
landDuration: number[],
235+
waterStartTime: number[],
236+
waterDuration: number[],
237+
): number {
238+
const x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
239+
const y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
240+
return Math.min(x, y);
241+
}
242+
243+
function calc(a1: number[], t1: number[], a2: number[], t2: number[]): number {
244+
let minEnd = Number.MAX_SAFE_INTEGER;
245+
for (let i = 0; i < a1.length; i++) {
246+
minEnd = Math.min(minEnd, a1[i] + t1[i]);
247+
}
248+
let ans = Number.MAX_SAFE_INTEGER;
249+
for (let i = 0; i < a2.length; i++) {
250+
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
251+
}
252+
return ans;
253+
}
157254
```
158255

159256
<!-- tabs:end -->

solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README_EN.md

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,32 +126,129 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3633.Ea
126126

127127
<!-- solution:start -->
128128

129-
### Solution 1
129+
### Solution 1: Enumeration + Greedy
130+
131+
We can consider two orders of rides: first land rides then water rides, or first water rides then land rides.
132+
133+
For each order, we first calculate the earliest end time $\textit{minEnd}$ of the first type of ride, then enumerate the second type of ride and calculate the earliest end time of the second type of ride as $\max(\textit{minEnd}, \textit{startTime}) + \textit{duration}$, where $\textit{startTime}$ is the start time of the second type of ride. We take the minimum value among all possible earliest end times as the answer.
134+
135+
Finally, we return the minimum value between the answers of the two orders.
136+
137+
The time complexity is $O(n + m)$, where $n$ and $m$ are the numbers of land rides and water rides respectively. The space complexity is $O(1)$.
130138

131139
<!-- tabs:start -->
132140

133141
#### Python3
134142

135143
```python
136-
144+
class Solution:
145+
def earliestFinishTime(self, landStartTime: List[int], landDuration: List[int], waterStartTime: List[int], waterDuration: List[int]) -> int:
146+
def calc(a1, t1, a2, t2):
147+
min_end = min(a + t for a, t in zip(a1, t1))
148+
return min(max(a, min_end) + t for a, t in zip(a2, t2))
149+
150+
x = calc(landStartTime, landDuration, waterStartTime, waterDuration)
151+
y = calc(waterStartTime, waterDuration, landStartTime, landDuration)
152+
return min(x, y)
137153
```
138154

139155
#### Java
140156

141157
```java
142-
158+
class Solution {
159+
public int earliestFinishTime(
160+
int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) {
161+
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
162+
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
163+
return Math.min(x, y);
164+
}
165+
166+
private int calc(int[] a1, int[] t1, int[] a2, int[] t2) {
167+
int minEnd = Integer.MAX_VALUE;
168+
for (int i = 0; i < a1.length; ++i) {
169+
minEnd = Math.min(minEnd, a1[i] + t1[i]);
170+
}
171+
int ans = Integer.MAX_VALUE;
172+
for (int i = 0; i < a2.length; ++i) {
173+
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
174+
}
175+
return ans;
176+
}
177+
}
143178
```
144179

145180
#### C++
146181

147182
```cpp
148-
183+
class Solution {
184+
public:
185+
int earliestFinishTime(vector<int>& landStartTime, vector<int>& landDuration, vector<int>& waterStartTime, vector<int>& waterDuration) {
186+
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
187+
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
188+
return min(x, y);
189+
}
190+
191+
int calc(vector<int>& a1, vector<int>& t1, vector<int>& a2, vector<int>& t2) {
192+
int minEnd = INT_MAX;
193+
for (int i = 0; i < a1.size(); ++i) {
194+
minEnd = min(minEnd, a1[i] + t1[i]);
195+
}
196+
int ans = INT_MAX;
197+
for (int i = 0; i < a2.size(); ++i) {
198+
ans = min(ans, max(minEnd, a2[i]) + t2[i]);
199+
}
200+
return ans;
201+
}
202+
};
149203
```
150204

151205
#### Go
152206

153207
```go
208+
func earliestFinishTime(landStartTime []int, landDuration []int, waterStartTime []int, waterDuration []int) int {
209+
x := calc(landStartTime, landDuration, waterStartTime, waterDuration)
210+
y := calc(waterStartTime, waterDuration, landStartTime, landDuration)
211+
return min(x, y)
212+
}
213+
214+
func calc(a1 []int, t1 []int, a2 []int, t2 []int) int {
215+
minEnd := math.MaxInt32
216+
for i := 0; i < len(a1); i++ {
217+
minEnd = min(minEnd, a1[i]+t1[i])
218+
}
219+
ans := math.MaxInt32
220+
for i := 0; i < len(a2); i++ {
221+
ans = min(ans, max(minEnd, a2[i])+t2[i])
222+
}
223+
return ans
224+
}
225+
```
154226

227+
#### TypeScript
228+
229+
```ts
230+
function earliestFinishTime(
231+
landStartTime: number[],
232+
landDuration: number[],
233+
waterStartTime: number[],
234+
waterDuration: number[],
235+
): number {
236+
const x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
237+
const y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
238+
return Math.min(x, y);
239+
}
240+
241+
function calc(a1: number[], t1: number[], a2: number[], t2: number[]): number {
242+
let minEnd = Number.MAX_SAFE_INTEGER;
243+
for (let i = 0; i < a1.length; i++) {
244+
minEnd = Math.min(minEnd, a1[i] + t1[i]);
245+
}
246+
let ans = Number.MAX_SAFE_INTEGER;
247+
for (let i = 0; i < a2.length; i++) {
248+
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
249+
}
250+
return ans;
251+
}
155252
```
156253

157254
<!-- tabs:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int earliestFinishTime(vector<int>& landStartTime, vector<int>& landDuration, vector<int>& waterStartTime, vector<int>& waterDuration) {
4+
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
5+
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
6+
return min(x, y);
7+
}
8+
9+
int calc(vector<int>& a1, vector<int>& t1, vector<int>& a2, vector<int>& t2) {
10+
int minEnd = INT_MAX;
11+
for (int i = 0; i < a1.size(); ++i) {
12+
minEnd = min(minEnd, a1[i] + t1[i]);
13+
}
14+
int ans = INT_MAX;
15+
for (int i = 0; i < a2.size(); ++i) {
16+
ans = min(ans, max(minEnd, a2[i]) + t2[i]);
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func earliestFinishTime(landStartTime []int, landDuration []int, waterStartTime []int, waterDuration []int) int {
2+
x := calc(landStartTime, landDuration, waterStartTime, waterDuration)
3+
y := calc(waterStartTime, waterDuration, landStartTime, landDuration)
4+
return min(x, y)
5+
}
6+
7+
func calc(a1 []int, t1 []int, a2 []int, t2 []int) int {
8+
minEnd := math.MaxInt32
9+
for i := 0; i < len(a1); i++ {
10+
minEnd = min(minEnd, a1[i]+t1[i])
11+
}
12+
ans := math.MaxInt32
13+
for i := 0; i < len(a2); i++ {
14+
ans = min(ans, max(minEnd, a2[i])+t2[i])
15+
}
16+
return ans
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int earliestFinishTime(
3+
int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) {
4+
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
5+
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
6+
return Math.min(x, y);
7+
}
8+
9+
private int calc(int[] a1, int[] t1, int[] a2, int[] t2) {
10+
int minEnd = Integer.MAX_VALUE;
11+
for (int i = 0; i < a1.length; ++i) {
12+
minEnd = Math.min(minEnd, a1[i] + t1[i]);
13+
}
14+
int ans = Integer.MAX_VALUE;
15+
for (int i = 0; i < a2.length; ++i) {
16+
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def earliestFinishTime(
3+
self,
4+
landStartTime: List[int],
5+
landDuration: List[int],
6+
waterStartTime: List[int],
7+
waterDuration: List[int],
8+
) -> int:
9+
def calc(a1, t1, a2, t2):
10+
min_end = min(a + t for a, t in zip(a1, t1))
11+
return min(max(a, min_end) + t for a, t in zip(a2, t2))
12+
13+
x = calc(landStartTime, landDuration, waterStartTime, waterDuration)
14+
y = calc(waterStartTime, waterDuration, landStartTime, landDuration)
15+
return min(x, y)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function earliestFinishTime(
2+
landStartTime: number[],
3+
landDuration: number[],
4+
waterStartTime: number[],
5+
waterDuration: number[],
6+
): number {
7+
const x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
8+
const y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
9+
return Math.min(x, y);
10+
}
11+
12+
function calc(a1: number[], t1: number[], a2: number[], t2: number[]): number {
13+
let minEnd = Number.MAX_SAFE_INTEGER;
14+
for (let i = 0; i < a1.length; i++) {
15+
minEnd = Math.min(minEnd, a1[i] + t1[i]);
16+
}
17+
let ans = Number.MAX_SAFE_INTEGER;
18+
for (let i = 0; i < a2.length; i++) {
19+
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
20+
}
21+
return ans;
22+
}

0 commit comments

Comments
 (0)