Solo practiceA little space to think.
A HACKERBREWERY ORIGINAL

The long way home

HardGraphsShortest paths

Plan a delivery through a directed network of n stops, numbered 0 to n − 1. Each edge is [from, to, cost], with a nonnegative cost. Return the cheapest total cost from start to end, or −1 if no route exists. Traveling from a stop to itself costs zero.

Example 1

INPUT4, [[0,1,4],[0,2,1],[2,1,1],[1,3,2]], 0, 3OUTPUT4

Travel 0 → 2 → 1 → 3, for a total cost of 1 + 1 + 2.

Example 2

INPUT3, [[0,1,2]], 0, 2OUTPUT-1

There is no route to stop 2.

Keep in mind

  • 1 ≤ n ≤ 1,000
  • 0 ≤ edges.length ≤ 5,000
  • 0 ≤ cost ≤ 10,000
  • start and end are valid stop numbers.
Your workspace
JavaScript
Draft saved on this browserSpaces: 2 UTF-8
Try an idea. See what happens.
INPUT4, [[0,1,4],[0,2,1],[2,1,1],[1,3,2]], 0, 3
EXPECTED OUTPUT4

Public samples only. These checks do not award ranked points.

The vibe
Your problem set 2 selected

Local preview · up to 6 tabs in this browser

A perfect pairArrays · Hash mapsEasyKeep it togetherStrings · StacksEasyTwo queues, one counterArrays · Two pointersEasySomething differentStrings · Sliding windowMediumIslands in the brewGraphs · Depth-first searchMediumThe long way homeGraphs · Shortest pathsHard
HackerBrewery

This is an early, local preview. Create a room, open its link in another tab in this browser, and try a session together. Room chat and sample progress update between those tabs.

Links do not connect different devices yet. Networked rooms, WebRTC, accounts, and ranked judging are still being built. Sample checks are public practice checks, not ranked verdicts.

Choose the name other tabs in your local room will see.