Knight Dialer

A chess knight stands on a phone keypad (digits 1-9 in three rows, 0 centered below them). It can only stand on numeric keys and moves like a knight: two squares in one direction and one square perpendicular. Given an integer n, return how many distinct phone numbers of length n you can dial by placing the knight on any numeric key and making n - 1 valid knight jumps. Return the answer modulo 10^9 + 7.

30:00

Knight Dialer
medium
Free written solution
Topics
Companies

A chess knight stands on a phone keypad (digits 1-9 in three rows, 0 centered below them). It can only stand on numeric keys and moves like a knight: two squares in one direction and one square perpendicular. Given an integer n, return how many distinct phone numbers of length n you can dial by placing the knight on any numeric key and making n - 1 valid knight jumps. Return the answer modulo 10^9 + 7.

Example 1:
Input: {"n":1}
Output: 10
Constraints:
  • 1≤n≤50001 \leq n \leq 5000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":1}

Every key is a valid 1-digit number, so each key starts with a count of 1 path ending on it.

n = 1 · counting 1-digit numbers

total = 10

0

1

1

1

2

1

3

1

4

1

5

1

6

1

7

1

8

1

9

1

*

#

grey = paths of length 1 ending here

♞ = key being spread

Variables
VariableValue
n1
len1
total10
DepthFunction Call
Stack empty
0/4