LeetCode 935, Medium. Topics: Dynamic Programming. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 17 parametrized pytest cases, and a playground notebook:
Problem
<p>The chess knight has a <strong>unique movement</strong> ,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 <strong>L</strong>). The possible movements of chess knight are shown in this diagram:</p>
<p>A chess knight can move as indicated in the chess diagram below:</p>
<p>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).</p>
<p><img alt="" src=“https://assets.leetcode.com/uploads/2020/08/18/chess.jpg” style=“width: 402px; height: 402px;” /></p>
<p><img alt="" src=“https://assets.leetcode.com/uploads/2020/08/18/phone.jpg” style=“width: 282px; height: 302px;” /></p>
<p>Given an integer <code>n</code>, return how many distinct phone numbers of length <code>n</code> we can dial.</p>
<p>You are allowed to place the knight on any numeric cell initially and then you should perform <code>n - 1</code> jumps to dial a number of length <code>n</code>. All jumps should be valid knight jumps.</p>
<p>As the answer may be very large, return the answer modulo <code>10<sup>9</sup> + 7</code>.</p>
Examples
Constraints
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026