Skip to the code
cf.
+25 XP

#489 Onboarding Flow Path Count

medium

An onboarding flow has n screens numbered 0 to n - 1. Each pair [from, to] in links is a button on screen from that opens screen to. Links never lead back to an earlier point of a flow, so there are no cycles, and no link appears twice.

Write countFlowPaths(n, links) that returns how many different paths lead from screen 0 to screen n - 1. If n is 1, the start is already the end, which counts as one path.

The count can be very large, so return it modulo 1000000007.

Example 1

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

0 → 1 → 3 and 0 → 2 → 3.

Example 2

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

Two ways to reach screen 2, then two ways from 2 to 4.

Constraints

  • 1 <= n <= 2000
  • 0 <= links.length <= 10000
  • The links form no cycle.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

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