Minimum Path Cost with Edge Discounts
Updated
Problem statement
You are given an undirected graph with nodes 0 through n - 1. Each edge [u, v, weight] has a positive integer cost.
Travel from node 0 to node n - 1. You may apply at most discounts discounts, each to a different traversal of an edge; a discounted edge costs floor(weight / 2). Return the minimum possible path cost, or -1 when the destination is unreachable.
Function
minimumPathWithDiscounts(n: int, edges: int[][], discounts: int) → longExamples
Example 1
n = 5edges = [[0,1,4],[1,4,8],[0,2,2],[2,3,2],[3,4,10]]discounts = 1return = 8Path 0-1-4 costs 4 + floor(8/2) = 8.
Example 2
n = 3edges = [[0,1,5]]discounts = 2return = -1Node 2 is unreachable.
Constraints
2 <= n <= 10000.0 <= edges.length <= 50000.1 <= weight <= 1000000000.0 <= discounts <= 20.
Similar questions at other companies
- Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
- Fuel Route with Fewest Station VisitsGoogle · ONSITE INTERVIEWGoogle interview questions
- Cheapest Flight with Its PathAirbnb · OA · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Maximum Gain on a Budgeted Returning WalkNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Path With Maximum ProbabilitySalesforce · ONSITE INTERVIEWSalesforce interview questions
- Cheapest Flights Within K StopsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Dijkstra's Shortest PathsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Single-Source Shortest PathsRippling · ONSITE INTERVIEWRippling interview questions
- Minimum-Weight Path Through a MatrixMeta · ONSITE INTERVIEWMeta interview questions