Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple Recursion :-) solution in Clear category for The Cheapest Flight by kdim
from typing import List
def cheapest_flight(costs: List, a: str, b: str) -> int:
res = [] # array with distance
for i, cost in enumerate(costs):
match = cost[:-1][cost.index(a)-1] if a in cost else '' # 'B' if 'A' in ['B','A',20] else ''
if match == b: # if the destination, then end of recursion
res += [cost[2]]
ret = cheapest_flight(costs[:i]+costs[i+1:], match, b) # else go throw recursion :-)
if ret:
res += [cost[2] + ret]
return min(res) if res else 0
Jan. 19, 2021
Comments: