351.Android Unlock Patterns

Tags: [graph], [grid], [DFS], [trick], [symmetric]

Com: {g}

Link: https://leetcode.com/problems/android-unlock-patterns/\#/description

Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.

Rules for a valid pattern:

  1. Each pattern must connect at least m keys and at most n keys.
  2. All the keys must be distinct.
  3. If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.
  4. The order of keys used matters.

Explanation:

| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |

Invalid move:4 - 1 - 3 - 6
Line 1 - 3 passes through key 2 which had not been selected in the pattern.

Invalid move:4 - 1 - 9 - 2
Line 1 - 9 passes through key 5 which had not been selected in the pattern.

Valid move:2 - 4 - 1 - 3 - 6
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern

Valid move:6 - 5 - 4 - 1 - 9 - 2
Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.

Example:
Given m= 1, n= 1, return 9.


Solution: DFS, Tick

class Solution(object):
    def numberOfPatterns(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        skip = [[0 for _ in xrange(10)] for _ in xrange(10)]
        skip[1][3] = skip[3][1] = 2
        skip[1][7] = skip[7][1] = 4
        skip[3][9] = skip[9][3] = 6
        skip[7][9] = skip[9][7] = 8
        skip[1][9] = skip[9][1] = skip[3][7] = skip[7][3] = skip[2][8] = skip[8][2] = skip[4][6] = skip[6][4] = 5

        visited = [False for _ in xrange(10)]

        counter = 0
        for num_of_keys in xrange(m, n + 1):
            counter += self.dfs(skip, num_of_keys, 1, 1, visited) * 4 # 1, 3, 7, 9 are symmetric
            counter += self.dfs(skip, num_of_keys, 2, 1, visited) * 4 # 2, 4, 8, 6 are sysmetric
            counter += self.dfs(skip, num_of_keys, 5, 1, visited)

        return counter

    def dfs(self, skip, num_of_keys, curr, curr_num_of_keys, visited):
        # base case
        if curr_num_of_keys > num_of_keys:
            return 0
        if curr_num_of_keys == num_of_keys:
            return 1

        visited[curr] = True

        counter = 0
        for child in xrange(1, 10):
            if not visited[child] and (skip[curr][child] == 0 or visited[skip[curr][child]]):
                counter += self.dfs(skip, num_of_keys, child, curr_num_of_keys + 1, visited)

        visited[curr] = False
        return counter

Revelation:

  • Because there are only 9 cells, so we can using a two dimension matrix to record all skips.
  • The slots 1, 3, 7, 9 are symmetric and slots 2, 4, 6, 8 are symmetric. Because we just want to the number of paths, so for each group we just need calculate one case, and then multiply 4.

Note:

  • Time complexity = O((n - m + 1) * time cost of DFS) = O(n - m), because the time cost of DFS is O(1), there are only 9 cells.

results matching ""

    No results matching ""