Watch 10 video solutions for Knight Dialer, a medium level problem involving Dynamic Programming. This walkthrough by NeetCodeIO has 12,151 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000Problem Overview: A knight moves on a classic phone keypad (0–9). Starting from any digit, the knight makes n-1 moves. Each move must follow chess knight rules. The task is to count how many distinct number sequences of length n can be dialed. Results are returned modulo 1e9+7.
Approach 1: Dynamic Programming (O(n) time, O(n) space)
The keypad can be modeled as a small directed graph where each digit connects to the digits reachable by a knight move. For example, 1 → {6,8}, 4 → {0,3,9}, and 5 has no valid moves. Use Dynamic Programming where dp[i][d] represents the number of sequences of length i ending at digit d. Initialize all digits with value 1 for length 1. For every step, iterate through digits and distribute counts to their reachable neighbors. The key insight is that the number of ways to reach a digit depends only on the previous step’s reachable digits. This transforms a potentially exponential search into a linear DP transition. Time complexity is O(n) because the digit set is constant (10), and space complexity is O(n) if you keep the full DP table. This approach clearly demonstrates state transitions and is easy to reason about during interviews. The technique is a classic example of Dynamic Programming applied to a small graph transition system.
Approach 2: Optimized Dynamic Programming with Space Reduction (O(n) time, O(1) space)
The DP state for step i depends only on step i-1. Storing the entire n × 10 table is unnecessary. Instead, keep two arrays: one for the current counts and one for the next counts. After computing transitions for all digits, swap them and continue. This reduces memory usage from O(n) to constant O(10), effectively O(1). Each iteration still processes a fixed set of knight transitions, so time remains O(n). This version is typically what production code or competitive programming solutions use. The logic is identical to the full DP table but relies on rolling state updates, a common pattern in dynamic programming problems where only the previous row matters.
Recommended for interviews: Start by describing the keypad as a graph of knight transitions. Explain the DP state dp[i][digit] and the recurrence that accumulates counts from reachable digits. Once the interviewer agrees with the state transition, mention the rolling-array optimization that reduces memory to O(1). Demonstrating both the straightforward DP and the space-optimized version shows strong understanding of DP state reduction patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming (Full DP Table) | O(n) | O(n) | Best for explaining the DP state transition clearly in interviews |
| Optimized DP with Rolling Array | O(n) | O(1) | Preferred implementation when memory efficiency matters |