Skip to the code
cf.
+50 XP

#660 Click Paths of Exact Length

hard

An analytics dashboard models a site as n pages numbered 0 to n - 1. links lists one-way links [from, to]; a page may link to itself. A visitor starts on page 0 and clicks a link exactly k times.

Write countClickPaths(n, links, k) that returns how many different click sequences end on page n - 1, modulo 1000000007. With k = 0 the visitor never moves, so the count is 1 if page 0 is page n - 1 and 0 otherwise.

Example 1

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

In one click, only the direct link 0 → 2 reaches the last page.

Example 2

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

In two clicks, only 0 → 1 → 2 works.

Constraints

  • 1 <= n <= 10
  • 0 <= links.length <= n * n, no repeated links
  • 0 <= k <= 10^9
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).