Skip to the code
+25 XP

#445 Count Routes Through a Pipeline

medium

A data pipeline has n stages numbered 0 to n - 1. Each edges[i] = [a, b] means records can flow from stage a to stage b. The edges never form a loop. Stage 0 is the input and stage n - 1 is the output.

Write countRoutes(n, edges) that returns how many different routes go from stage 0 to stage n - 1. Two routes are different if their lists of stages differ. If n = 1, the single stage is one route.

Example 1

Input: n = 4, edges = [[0,1],[0,2],[1,3],[2,3]]
Output: 2

The routes are 0 → 1 → 3 and 0 → 2 → 3.

Example 2

Input: n = 3, edges = [[0,1]]
Output: 0

Stage 2 can't be reached at all.

Constraints

  • 1 <= n <= 60
  • 0 <= edges.length <= 500
  • No edge is repeated, and the answer is below 2^53.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).