Earliest Finish Time for Land and Water Rides I - Video Solutions
Earliest Finish Time for Land and Water Rides I and II | Story To Code | Leetcode 3633 & 3635 | MIK
Earliest Finish Time for Land and Water Rides I - Video Solution
Watch 10 video solutions for Earliest Finish Time for Land and Water Rides I, a easy level problem involving Array, Two Pointers, Binary Search. This walkthrough by codestorywithMIK has 8,928 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Statement
You are given two categories of theme park attractions: land rides and water rides.
- Land rides
landStartTime[i]– the earliest time theithland ride can be boarded.landDuration[i]– how long theithland ride lasts.
- Water rides
waterStartTime[j]– the earliest time thejthwater ride can be boarded.waterDuration[j]– how long thejthwater 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 timet + 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.
Example 1:
Input: landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]
Output: 9
Explanation:βββββββ
- Plan A (land ride 0 → water ride 0):
- Start land ride 0 at time
landStartTime[0] = 2. Finish at2 + landDuration[0] = 6. - Water ride 0 opens at time
waterStartTime[0] = 6. Start immediately at6, finish at6 + waterDuration[0] = 9.
- Start land ride 0 at time
- Plan B (water ride 0 → land ride 1):
- Start water ride 0 at time
waterStartTime[0] = 6. Finish at6 + waterDuration[0] = 9. - Land ride 1 opens at
landStartTime[1] = 8. Start at time9, finish at9 + landDuration[1] = 10.
- Start water ride 0 at time
- Plan C (land ride 1 → water ride 0):
- Start land ride 1 at time
landStartTime[1] = 8. Finish at8 + landDuration[1] = 9. - Water ride 0 opened at
waterStartTime[0] = 6. Start at time9, finish at9 + waterDuration[0] = 12.
- Start land ride 1 at time
- Plan D (water ride 0 → land ride 0):
- Start water ride 0 at time
waterStartTime[0] = 6. Finish at6 + waterDuration[0] = 9. - Land ride 0 opened at
landStartTime[0] = 2. Start at time9, finish at9 + landDuration[0] = 13.
- Start water ride 0 at time
Plan A gives the earliest finish time of 9.
Example 2:
Input: landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]
Output: 14
Explanation:βββββββ
- Plan A (water ride 0 → land ride 0):
- Start water ride 0 at time
waterStartTime[0] = 1. Finish at1 + waterDuration[0] = 11. - Land ride 0 opened at
landStartTime[0] = 5. Start immediately at11and finish at11 + landDuration[0] = 14.
- Start water ride 0 at time
- Plan B (land ride 0 → water ride 0):
- Start land ride 0 at time
landStartTime[0] = 5. Finish at5 + landDuration[0] = 8. - Water ride 0 opened at
waterStartTime[0] = 1. Start immediately at8and finish at8 + waterDuration[0] = 18.
- Start land ride 0 at time
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] <= 1000
Approach Overview
Problem 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.
Complexity Analysis
| 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 |