Ski Path

Asked at Airbnb

Reported by the problem's creator, not verified

A ski resort is a one-way trail map. travel lists the lifts and runs as [from, cost, to]: moving from node from to node to costs cost energy. points lists [node, reward]: arriving at node earns reward points. A node that is not in points earns 0, and the starting node, named "start", earns nothing because a skier begins there rather than arriving. The same from and to pair can appear more than once with different costs. The map has no cycles, so no node repeats on a run.

A run begins at "start" and may end at any node whose id begins with END; it can pass through such a node and keep going. Its score is the total reward of the nodes it reaches (each once, the start excluded) minus the total cost of the edges it takes.

Write best_ski_score(travel, points), returning the highest score over all runs from "start" to an END node. Costs and rewards are given as integers.

Examples

  1. Example 1

    Input: {"travel":[["start",3,"A"],["A",4,"B"],["B",5,"END1"]],"points":[["A",5],["B",6],["END1",3]]}

    Output: 2

    Explanation: The only path is start, A, B, END1. Rewards: 5 + 6 + 3 = 14. Costs: 3 + 4 + 5 = 12. The score is 14 - 12 = 2.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.