Problem · Array
Cheapest Round Trip
Learn this problemProblem 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) → longExamples
Example 1
departureFares = [8,3,6,2]returnFares = [9,5,4,7]minStay = 1return = 7Depart 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 = 3Depart 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.