An Android lock screen shows a 3 x 3 grid of dots. A user sets an unlock pattern by connecting dots in sequence, which forms a chain of line segments where each segment joins two consecutive dots of the sequence.
A sequence of k dots is a valid unlock pattern when both of these hold:
-
All dots in the sequence are distinct.
-
If the segment between two consecutive dots passes through the center of another dot, that other dot must have appeared earlier in the sequence. A segment that passes through no other dot's center is always allowed.
Given two integers m and n, return the number of distinct valid unlock patterns that use at least m dots and at most n dots.
Function Signature
def number_of_patterns(m: int, n: int) -> int:
Rules
-
The dots are numbered 1 to 9 row by row:
1 2 3
4 5 6
7 8 9
-
A pattern is an ordered sequence, so
[1, 2]
and
[2, 1]
are two different patterns.
-
A segment passes through the center of another dot only when that dot sits exactly at the segment's midpoint, for example 1 to 3 (through 2), 4 to 6 (through 5) or 1 to 9 (through 5). A move such as 1 to 6 or 1 to 8 passes through no dot center and is always allowed.
-
A segment may pass over a dot that is already in the pattern.
Constraints
Examples
Input: m = 1, n = 1
Output: 9
Each of the 9 dots on its own is a one-dot pattern.
Input: m = 1, n = 2
Output: 65
There are 9 one-dot patterns and 56 two-dot patterns. Of the 72 ordered pairs of distinct dots, only the 16 whose midpoint is an unused dot (such as 1 to 3) are invalid, and 72 minus 16 is 56.
Input: m = 3, n = 3
Output: 320
Only three-dot patterns count. Each corner start allows 31 of them, each edge start 37 and the center 48, so the total is 4 * 31 + 4 * 37 + 48 = 320.