15% off all plans & roadmaps Use code
See plansLeetCode 1976 · Solution & editorial
You are in a city that consists of n intersections numbered from 0 to n - 1 with bi-directional roads between some intersections. The inputs are generated such that you can reach any intersection from any other intersection and that there is at most one road between any two intersections.
Number of Ways to Arrive at Destination is a Graph Traversal Patterns (DFS & BFS) problem, in the Shortest Path (Dijkstra's Algorithm) subpattern. The pattern covers DFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
Master Dijkstra's algorithm for finding shortest paths in weighted graphs with non-negative edges.
The reference solution runs in O((|E| + |V|) * log|V|) time and O(|E| + |V|) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Number of Ways to Arrive at Destination is a Graph Traversal Patterns (DFS & BFS) problem, in the Shortest Path (Dijkstra's Algorithm) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.