Problem · Dynamic Programming
Knight Dialer Sequences
Learn this problemProblem statement
A chess knight is dialing numbers on the standard telephone keypad:
1 2 3
4 5 6
7 8 9
0A sequence may start on any digit. Each following digit must be reachable from the previous digit by one legal knight move. Given the sequence length n, return the number of valid digit sequences modulo 1000000007.
Function
countKnightDialerNumbers(n: int) → intExamples
Example 1
n = 1return = 10Every one-digit sequence is valid.
Example 2
n = 2return = 20There are twenty directed knight moves between keypad digits.
Example 3
n = 3return = 46Extending every valid two-digit sequence by one knight move produces forty-six sequences.
Constraints
1 <= n <= 5000.- A sequence may begin at any of the ten digits.
- Digit
5has no legal outgoing knight move.