Get Minimum Round Trip Cost
Updated
Problem statement
You are given two arrays of the same length, departing and returning. Each index is a time, and each value is the cost of a flight at that time: departing[i] is the cost of leaving at time i, and returning[j] is the cost of coming back at time j.
A round trip is one departing flight and one returning flight, and the return must be strictly later than the departure: if you leave at time i, you can only come back at a time j with j ≥ i + 1.
Return the minimum total cost departing[i] + returning[j] over all valid round trips.
Complete the function getMinimumRoundTripCost in the editor. It has the following parameters:
int[] departing: the cost of the departing flight at each timeint[] returning: the cost of the returning flight at each time
The function returns an int: the minimum cost of a round trip.
Function
getMinimumRoundTripCost(departing: int[], returning: int[]) → intExamples
Example 1
departing = [1, 2, 3, 4]returning = [4, 3, 2, 1]return = 2The minimum cost for a round trip can be obtained by taking the departing flight at time 0 with cost departing[0] = 1 and the returning flight at time 3 with cost returning[3] = 1. The total minimum cost is 1 + 1 = 2.
Constraints
2 ≤ n ≤ 105, wherenis the length ofdepartingreturning.length = departing.length1 ≤ departing[i] ≤ 1091 ≤ returning[i] ≤ 109
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Eating CandiesTikTok · OATikTok interview questions
- Running Through Walls with Limited EnergyScale AI · OAScale AI interview questions
- Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Count Hotspot Playback WindowsGoogle · ONSITE INTERVIEWGoogle interview questions
- Subarray with a Given SumExpedia · ONSITE INTERVIEWExpedia interview questions
- Trapping Rain WaterInMobi · ONSITE INTERVIEWInMobi interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Minimum Index Distance Between Person and CakeSnowflake · OA · PHONE SCREENSnowflake interview questions
- Minimum Positive Starting ValueBooking.com · OA