Number of Ways to Arrive at Destination — LeetCode 1976 Python Solution

MediumGraphTopological SortDynamic ProgrammingShortest Path
Problem
#1976
Reading time
5 min

The problem

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.

Example

Input
n = 7, roads = [[0,6,7],[0,1,2],[1,2,3],[1,3,3],[6,3,3],[3,5,1],[6,5,1],[2,5,1],[0,4,5],[4,6,2]]
Output
4
Explanation
The shortest amount of time it takes to go from intersection 0 to intersection 6 is 7 minutes.

Python solution

Python
class Solution:
    def countPaths(self, n: int, roads: List[List[int]]) -> int:
        g = [[inf] * n for _ in range(n)]
        for u, v, t in roads:
            g[u][v] = g[v][u] = t
        g[0][0] = 0
        dist = [inf] * n
        dist[0] = 0
        f = [0] * n
        f[0] = 1
        vis = [False] * n
        for _ in range(n):
            t = -1
            for j in range(n):
                if not vis[j] and (t == -1 or dist[j] < dist[t]):
                    t = j
            vis[t] = True
            for j in range(n):
                if j == t:
                    continue
                ne = dist[t] + g[t][j]
                if dist[j] > ne:
                    dist[j] = ne
                    f[j] = f[t]
                elif dist[j] == ne:
                    f[j] += f[t]
        mod = 10**9 + 7
        return f[-1] % mod

Complexity

MeasureComplexity
TimeO(n^2)
SpaceO(n^2), where n is the number of points auxiliary

Pattern: Topological Sort

Order a set of tasks so that every dependency comes before the thing that needs it. LeetCode 1976. Number of Ways to Arrive at Destination is filed here because LeetCode tags it Topological Sort, which is the vocabulary this hub collects.

The topological sort guide has the Python template for the pattern and the 32 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1976. Number of Ways to Arrive at Destination?
LeetCode 1976. Number of Ways to Arrive at Destination is rated Medium on LeetCode.
What is the time complexity of LeetCode 1976. Number of Ways to Arrive at Destination?
The Python solution on this page runs in O(n^2).
What is the space complexity of LeetCode 1976. Number of Ways to Arrive at Destination?
The Python solution on this page uses O(n^2), where n is the number of points auxiliary space.
What topics does LeetCode 1976. Number of Ways to Arrive at Destination cover?
LeetCode 1976. Number of Ways to Arrive at Destination is tagged Graph, Topological Sort, Dynamic Programming and Shortest Path on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview