Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 5 * 104landStartTime.length == landDuration.length == nwaterStartTime.length == waterDuration.length == m1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search · Greedy
[2,8] [4,1] [6] [3]
[5] [3] [1] [10]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3635: Earliest Finish Time for Land and Water Rides II
class Solution {
public int earliestFinishTime(
int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) {
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
return Math.min(x, y);
}
private int calc(int[] a1, int[] t1, int[] a2, int[] t2) {
int minEnd = Integer.MAX_VALUE;
for (int i = 0; i < a1.length; ++i) {
minEnd = Math.min(minEnd, a1[i] + t1[i]);
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < a2.length; ++i) {
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
}
return ans;
}
}
// Accepted solution for LeetCode #3635: Earliest Finish Time for Land and Water Rides II
func earliestFinishTime(landStartTime []int, landDuration []int, waterStartTime []int, waterDuration []int) int {
x := calc(landStartTime, landDuration, waterStartTime, waterDuration)
y := calc(waterStartTime, waterDuration, landStartTime, landDuration)
return min(x, y)
}
func calc(a1 []int, t1 []int, a2 []int, t2 []int) int {
minEnd := math.MaxInt32
for i := 0; i < len(a1); i++ {
minEnd = min(minEnd, a1[i]+t1[i])
}
ans := math.MaxInt32
for i := 0; i < len(a2); i++ {
ans = min(ans, max(minEnd, a2[i])+t2[i])
}
return ans
}
# Accepted solution for LeetCode #3635: Earliest Finish Time for Land and Water Rides II
class Solution:
def earliestFinishTime(
self,
landStartTime: List[int],
landDuration: List[int],
waterStartTime: List[int],
waterDuration: List[int],
) -> int:
def calc(a1, t1, a2, t2):
min_end = min(a + t for a, t in zip(a1, t1))
return min(max(a, min_end) + t for a, t in zip(a2, t2))
x = calc(landStartTime, landDuration, waterStartTime, waterDuration)
y = calc(waterStartTime, waterDuration, landStartTime, landDuration)
return min(x, y)
// Accepted solution for LeetCode #3635: Earliest Finish Time for Land and Water Rides II
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3635: Earliest Finish Time for Land and Water Rides II
// class Solution {
// public int earliestFinishTime(
// int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) {
// int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
// int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
// return Math.min(x, y);
// }
//
// private int calc(int[] a1, int[] t1, int[] a2, int[] t2) {
// int minEnd = Integer.MAX_VALUE;
// for (int i = 0; i < a1.length; ++i) {
// minEnd = Math.min(minEnd, a1[i] + t1[i]);
// }
// int ans = Integer.MAX_VALUE;
// for (int i = 0; i < a2.length; ++i) {
// ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3635: Earliest Finish Time for Land and Water Rides II
function earliestFinishTime(
landStartTime: number[],
landDuration: number[],
waterStartTime: number[],
waterDuration: number[],
): number {
const x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
const y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
return Math.min(x, y);
}
function calc(a1: number[], t1: number[], a2: number[], t2: number[]): number {
let minEnd = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < a1.length; i++) {
minEnd = Math.min(minEnd, a1[i] + t1[i]);
}
let ans = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < a2.length; i++) {
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.