Earliest Finish Time for Land and Water Rides I

easy array greedy enumeration

Problem

There are land rides and water rides. Land ride i opens at landStartTime[i] and lasts landDuration[i]; water ride j opens at waterStartTime[j] and lasts waterDuration[j]. A tourist must take exactly one ride from each category, in either order. A ride started at time t finishes at t + duration; the second ride begins as soon as it is open or right after the first ends, whichever is later. Return the earliest possible time to finish both rides.

InputlandStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]
Output9
Land ride 0: start at 2, finish at 6. Water ride 0 is open at 6, so start at 6 and finish at 9. No plan finishes earlier.

def earliestFinishTime(landStartTime, landDuration, waterStartTime, waterDuration):
    best = float("inf")
    # Try every (land ride, water ride) pair.
    for i in range(len(landStartTime)):
        for j in range(len(waterStartTime)):
            # Plan 1: land first, then water.
            land_done = landStartTime[i] + landDuration[i]
            plan1 = max(land_done, waterStartTime[j]) + waterDuration[j]
            # Plan 2: water first, then land.
            water_done = waterStartTime[j] + waterDuration[j]
            plan2 = max(water_done, landStartTime[i]) + landDuration[i]
            # Keep the better ordering for this pair.
            best = min(best, plan1, plan2)
    return best
function earliestFinishTime(landStartTime, landDuration, waterStartTime, waterDuration) {
  let best = Infinity;
  // Try every (land ride, water ride) pair.
  for (let i = 0; i < landStartTime.length; i++) {
    for (let j = 0; j < waterStartTime.length; j++) {
      // Plan 1: land first, then water.
      const landDone = landStartTime[i] + landDuration[i];
      const plan1 = Math.max(landDone, waterStartTime[j]) + waterDuration[j];
      // Plan 2: water first, then land.
      const waterDone = waterStartTime[j] + waterDuration[j];
      const plan2 = Math.max(waterDone, landStartTime[i]) + landDuration[i];
      // Keep the better ordering for this pair.
      best = Math.min(best, plan1, plan2);
    }
  }
  return best;
}
int earliestFinishTime(int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) {
    int best = Integer.MAX_VALUE;
    // Try every (land ride, water ride) pair.
    for (int i = 0; i < landStartTime.length; i++) {
        for (int j = 0; j < waterStartTime.length; j++) {
            // Plan 1: land first, then water.
            int landDone = landStartTime[i] + landDuration[i];
            int plan1 = Math.max(landDone, waterStartTime[j]) + waterDuration[j];
            // Plan 2: water first, then land.
            int waterDone = waterStartTime[j] + waterDuration[j];
            int plan2 = Math.max(waterDone, landStartTime[i]) + landDuration[i];
            // Keep the better ordering for this pair.
            best = Math.min(best, Math.min(plan1, plan2));
        }
    }
    return best;
}
int earliestFinishTime(vector<int>& landStartTime, vector<int>& landDuration, vector<int>& waterStartTime, vector<int>& waterDuration) {
    int best = INT_MAX;
    // Try every (land ride, water ride) pair.
    for (int i = 0; i < (int)landStartTime.size(); i++) {
        for (int j = 0; j < (int)waterStartTime.size(); j++) {
            // Plan 1: land first, then water.
            int landDone = landStartTime[i] + landDuration[i];
            int plan1 = max(landDone, waterStartTime[j]) + waterDuration[j];
            // Plan 2: water first, then land.
            int waterDone = waterStartTime[j] + waterDuration[j];
            int plan2 = max(waterDone, landStartTime[i]) + landDuration[i];
            // Keep the better ordering for this pair.
            best = min(best, min(plan1, plan2));
        }
    }
    return best;
}
Time: O(n · m) Space: O(1)