I recently took an interview at a company (starting with M and ending in A) which asked me this question. Still practicing my algorithms, so I was hoping someone could help me understand how to solve this problem, and these types of problems.
The problem:
You are given 2 arrays. For example:
D = [10,7,13,12,4]
R = [5,12,7,10,12]
D denotes the departure prices for flights from city A to city B. R denotes the return prices for flights from city B to city A. Find the minimum cost of a round trip flight between city A and city B. For example, the minimum in the example is D[1] + R[2].
(possible only to take the return flight on same or higher index from the departure flight)
The tricky part is that, obviously, you must depart before returning.
The naïve approach is just a double for loop combining all the possibilities. However, I know there is a better approach, but I can't wrap my head around it. I believe we want to create some sort of temporary array with the minimum so far or something like that...
Thanks for reading.