You are given two categories of theme park attractions: land rides and water rides.
landStartTime[i] – the earliest time the ith land ride can be boarded.landDuration[i] – how long the ith land ride lasts.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.
t, it finishes at time t + duration.Return the earliest possible time at which the tourist can finish both rides.
Example 1:
Input: landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]
Output: 9
Explanation:
landStartTime[0] = 2. Finish at 2 + landDuration[0] = 6.waterStartTime[0] = 6. Start immediately at 6, finish at 6 + waterDuration[0] = 9.waterStartTime[0] = 6. Finish at 6 + waterDuration[0] = 9.landStartTime[1] = 8. Start at time 9, finish at 9 + landDuration[1] = 10.landStartTime[1] = 8. Finish at 8 + landDuration[1] = 9.waterStartTime[0] = 6. Start at time 9, finish at 9 + waterDuration[0] = 12.waterStartTime[0] = 6. Finish at 6 + waterDuration[0] = 9.landStartTime[0] = 2. Start at time 9, finish at 9 + landDuration[0] = 13.Plan A gives the earliest finish time of 9.
Example 2:
Input: landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]
Output: 14
Explanation:
waterStartTime[0] = 1. Finish at 1 + waterDuration[0] = 11.landStartTime[0] = 5. Start immediately at 11 and finish at 11 + landDuration[0] = 14.landStartTime[0] = 5. Finish at 5 + landDuration[0] = 8.waterStartTime[0] = 1. Start immediately at 8 and finish at 8 + waterDuration[0] = 18.Plan A provides the earliest finish time of 14.
Constraints:
1 <= n, m <= 100landStartTime.length == landDuration.length == nwaterStartTime.length == waterDuration.length == m1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 1000Problem Overview: You must take exactly one land ride and one water ride. Each ride has an available start time and a duration. After finishing the land ride, you choose a water ride and may need to wait until it becomes available. The goal is to minimize the final completion time.
Approach 1: Brute Force Enumeration (O(n * m) time, O(1) space)
Try every possible pair of land and water rides. For each land ride i, compute when you finish it: finishLand = landStart[i] + landDuration[i]. Then iterate through every water ride j and compute the actual start time startWater = max(finishLand, waterStart[j]). The total completion time becomes startWater + waterDuration[j]. Track the minimum across all pairs. This approach is straightforward and demonstrates the core scheduling logic, but it becomes slow when both ride lists are large.
Approach 2: Enumeration + Greedy with Sorting and Binary Search (O((n + m) log m) time, O(m) space)
Instead of scanning every water ride for each land ride, sort water rides by their start time. For each land ride, compute finishLand and use binary search to find the first water ride whose start time is greater than or equal to this value. That ride lets you start immediately without extra waiting. For water rides that start earlier than finishLand, you can still take them but must wait until the land ride finishes. Precomputing the best candidate (such as minimum duration or minimum finish time) in suffix arrays lets you quickly determine the optimal choice after the binary search. This reduces the repeated scanning and turns the nested loop into a logarithmic lookup.
This method combines simple enumeration of land rides with a greedy choice among water rides. Sorting enables fast lookups, while binary search identifies the earliest feasible candidate. The technique is common in scheduling problems where one event must follow another.
Related concepts appear frequently in problems involving arrays, binary search, and greedy scheduling with sorted events. Two-pointer variants can further optimize scanning when both lists are processed in order.
Recommended for interviews: The enumeration + greedy approach with sorting and binary search is the expected solution. Interviewers often accept brute force as a starting point, but optimizing it with sorted events and fast lookups demonstrates strong problem‑solving and algorithmic thinking.
We can consider two orders of rides: first land rides then water rides, or first water rides then land rides.
For each order, we first calculate the earliest end time 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(minEnd, startTime) + duration, where 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.
Finally, we return the minimum value between the answers of the two orders.
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).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n * m) | O(1) | Small inputs or when first reasoning about the scheduling logic |
| Enumeration + Greedy with Sorting & Binary Search | O((n + m) log m) | O(m) | Preferred approach for interviews and large datasets |
| Two Pointers on Sorted Rides | O(n + m) | O(1) | When both ride lists are processed in sorted order and you scan them simultaneously |
Leetcode Biweekly Contest 162 | Earliest-finish-time-for-land-and-water-rides-i | #leetcodecontest • TFI Developer • 313 views views
Watch 4 more video solutions →Practice Earliest Finish Time for Land and Water Rides I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor