Problem · Array

Cheapest Round Trip

Learn this problem
EasyUber logoUberFULLTIMEONSITE INTERVIEW
See Uber hiring insights

Problem statement

Two equal-length arrays describe fares by day: departureFares[i] is the cost to depart on day i, and returnFares[j] is the cost to return on day j.

Given a positive integer minStay, choose days satisfying j - i >= minStay and return the minimum value of departureFares[i] + returnFares[j].

Function

minimumRoundTripFare(departureFares: int[], returnFares: int[], minStay: int) → long

Examples

Example 1

departureFares = [8,3,6,2]returnFares = [9,5,4,7]minStay = 1return = 7

Depart on day 1 for 3 and return on day 2 for 4.

Example 2

departureFares = [10,2,8,4,9]returnFares = [6,7,3,5,1]minStay = 3return = 3

Depart on day 1 for 2 and return on day 4 for 1.

Constraints

  • The two fare arrays have the same length of at least 2.
  • 1 <= minStay < departureFares.length.
  • Every fare is a nonnegative signed integer.

More Uber problems

drafts saved locally
public long minimumRoundTripFare(int[] departureFares, int[] returnFares, int minStay) {
    // TODO: return the minimum valid departure-plus-return fare.
}
departureFares[8,3,6,2]
returnFares[9,5,4,7]
minStay1
expected7
checking account