1802. Unlock Pattern Count
A phone lock screen shows a 3 x 3 keypad with dots 1 to 9 numbered row by row, so 1 2 3 is the top row and 7 8 9 the bottom row. A pattern is a sequence of distinct dots, and each step draws a straight segment from the previous dot to the next one. A segment that passes straight over a third dot (for example 1 to 3 over 2, 1 to 9 over 5, or 2 to 8 over 5) is allowed only when that middle dot was already used earlier in the pattern or is listed in broken. Every other segment is always allowed. Dots in broken are damaged and can never be part of a pattern.
Return how many patterns have a length (number of dots) between minLen and maxLen inclusive. Patterns with the same dots in a different order are different. Backtrack with a used-dot mask and a skip table, caching on (mask, last dot).
Example 1
- Input:
- minLen = 1, maxLen = 1, broken = []
- Output:
- 9
- Explanation:
Each of the 9 working dots is a pattern of length 1.
Example 2
- Input:
- minLen = 2, maxLen = 2, broken = []
- Output:
- 56
- Explanation:
Of the 72 ordered pairs of distinct dots, 16 jump over an unused middle dot, so 56 remain.
Example 3
- Input:
- minLen = 3, maxLen = 3, broken = [5]
- Output:
- 256
- Explanation:
With dot 5 broken it no longer blocks the four lines through the centre, and exactly 256 patterns of 3 dots exist.
Constraints
1 ≤ minLen ≤ maxLen ≤ 9
broken holds distinct dots in 1..9 and may be empty
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(2^9 * 9 * 9)
- Space
- O(2^9 * 9)