[LeetCode] Earliest Finish Time for Land and Water Rides I

3633. Earliest Finish Time for Land and Water Rides I

You are given two categories of theme park attractions: land rides and water rides.

  • Land rides
    • landStartTime[i] – the earliest time the ith land ride can be boarded.
    • landDuration[i] – how long the ith land ride lasts.
  • Water rides
    • waterStartTime[j] – the earliest time the jth water ride can be boarded.
    • waterDuration[j] – how long the jth water ride lasts.

A tourist must experience exactly one ride from each category, in either order.

  • A ride may be started at its opening time or any later moment.
  • If a ride is started at time t, it finishes at time t + duration.
  • Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.

Return the earliest possible time at which the tourist can finish both rides.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
int helper(vector<pair<int,int>>& A, vector<pair<int,int>>& B) {
int end = INT_MAX;
for(auto& [start, fin] : A) end = min(fin, end);
int res = INT_MAX;
for(auto& [start, fin] : B) {
int ride = max(fin, fin - start + end);
res = min(res, ride);
}
return res;
}
public:
int earliestFinishTime(vector<int>& landStartTime, vector<int>& landDuration, vector<int>& waterStartTime, vector<int>& waterDuration) {
vector<pair<int,int>> land, water;
for(int i = 0; i < landStartTime.size(); i++) {
land.push_back({landStartTime[i], landStartTime[i] + landDuration[i]});
}
for(int i = 0; i < waterStartTime.size(); i++) {
water.push_back({waterStartTime[i], waterStartTime[i] + waterDuration[i]});
}
return min(helper(land, water), helper(water, land));
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/08/03/PS/LeetCode/earliest-finish-time-for-land-and-water-rides-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.