Code RoomShortest path Dijkstra
MediumPrep Room Coding #1348

Shortest path Dijkstra

CodingAlgorithms & data structuresMid–Senior~30 min

Given n nodes labeled 0..n-1 and a list of directed weighted edges [u, v, w] with w >= 0, find the shortest-path distance from node `src` to every node. Return a list of length n where index i is the distance to node i, or -1 if unreachable. Use a priority queue. The graph may have multiple edges between the same pair.

Implement
dijkstra(n: int, edges: list[list[int]], src: int) → list[int]
Examples
in[5,[[0,1,4],[0,2,1],[2,1,2],[1,3,1],[2,3,5]],0]out[0,3,1,4,-1]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[5,[[0,1,4],[0,2,1],[2,1,2],[1,3,1],[2,3,5]],0][0,3,1,4,-1]not run yetsample