#3633
Easy Algorithms Earliest finish time for land and water rides i
Array Two Pointers Binary Search Greedy Sorting
61.7% acceptance
Feb 25, 2026
67
16
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.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn earliest_finish_time(
land_start_time: Vec<i32>,
land_duration: Vec<i32>,
water_start_time: Vec<i32>,
water_duration: Vec<i32>,
) -> i32 {
let mut best = i32::MAX;
for i in 0..land_start_time.len() {
let land_end = land_start_time[i] + land_duration[i];
for j in 0..water_start_time.len() {
let water_end = water_start_time[j] + water_duration[j];
// land then water
let t1 = land_end.max(water_start_time[j]) + water_duration[j];
// water then land
let t2 = water_end.max(land_start_time[i]) + land_duration[i];
best = best.min(t1).min(t2);
}
}
best
}
}