Stripe OA 面试真题解析:最多经过一个中转国家的 shipping routes

19次阅读
没有评论

Modify your program such that it can find shipping routes through at most one intermediate country. Any shipping methods are allowed. Output the route, the method(s) taken, and the total cost.

For instance, shippingRoute(inputString, "US", "FR") should return:

{
  route: "US -> UK -> FR",
  method: "FedEx -> DHL",
  cost: 7
}

这题要求在给定的国家与运输方式网络中,找出从起点到终点的最优 shipping route,且路径最多只能经过一个中转国家。输出不仅要包含完整路线,还要包含每一段所使用的运输方式以及总成本。解题时可以把国家看成图中的节点、运输方式看成带权边;由于只允许 0 或 1 个中转点,通常可以直接枚举直达和两段路径,或者用图遍历 / 最短路思路去筛选候选路径,再比较总费用并拼接 route 与 method 字符串。

正文完
 0