A mobile app's navigation map has n screens numbered 0 to n - 1. Each moves[i] = [from, to, cost] means you can go from screen from to screen to, and cost is how many taps that transition takes.
Write cheapestRoute(n, moves, from, to) that returns the fewest total taps to get from screen from to screen to, or -1 if it can't be done. If from === to, the answer is 0.
Input: n = 4, moves = [[0,1,5],[0,2,1],[2,1,1],[1,3,1]], from = 0, to = 3 Output: 3
Going 0 → 2 → 1 → 3 costs 1 + 1 + 1 = 3, cheaper than 0 → 1 → 3 at 6.
Input: n = 3, moves = [[0,1,2]], from = 0, to = 2 Output: -1
No move leads to screen 2.
1 <= n <= 10000 <= moves.length <= 100001 <= cost <= 100Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #460 Cheapest Route Between Two Screens. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.