Wednesday, March 3, 2021

[Google Question][LeetCode] Android Unlock Patterns

Problem: Android devices have a special lock screen with a 3 x 3 grid of dots. Users can set an "unlock pattern" by connecting the dots in a specific sequence, forming a series of joined line segments where each segment's endpoints are two consecutive dots in the sequence. A sequence of k dots is a valid unlock pattern if both of the following are true:

All the dots in the sequence are distinct.

If the line segment connecting two consecutive dots in the sequence passes through any other dot, the other dot must have previously appeared in the sequence. No jumps through non-selected dots are allowed.

Here are some example valid and invalid unlock patterns:

  • The 1st pattern [4,1,3,6] is invalid because the line connecting dots 1 and 3 pass through dot 2, but dot 2 did not previously appear in the sequence.
  • The 2nd pattern [4,1,9,2] is invalid because the line connecting dots 1 and 9 pass through dot 5, but dot 5 did not previously appear in the sequence.
  • The 3rd pattern [2,4,1,3,6] is valid because it follows the conditions. The line connecting dots 1 and 3 meets the condition because dot 2 previously appeared in the sequence.
  • The 4th pattern [6,5,4,1,9,2] is valid because it follows the conditions. The line connecting dots 1 and 9 meets the condition because dot 5 previously appeared in the sequence.

Given two integers m and n, return the number of unique and valid unlock patterns of the Android grid lock screen that consist of at least m keys and at most n keys.

Two unlock patterns are considered unique if there is a dot in one sequence that is not in the other, or the order of the dots is different.

Example:

Input: m = 1, n = 1
Output: 9
Input: m = 1, n = 2
Output: 65


Approach: We need to use backtracking / DFS here. Basically for every length m - n we will start with every digit (1 - 9) as starting point, we apply DFS and see how many patterns we can get for a particular length. We can choose next number / digit if:

  • It is not visited
  • Mid digit is visited in case of overlap.

Please note that in case of knight movement, there are no overlaps. 

To optimize it if we look closely at the patterns, we will find that all the corner digits 1, 3, 7, 9 are having same number of patterns. Similarly all the mid digits 2, 4, 6, 8 have the same number of patterns so we can just calculate 1 of the number is in these two groups and can multiply it by 4. In that way we save some time.


Implementation in C#:

    public int NumberOfPatterns(int m, int n) 

    {

        int numOfPatterns = 0;

        int[,] midNumMap = this.BuildMidNumMap();

        int[] startNums = {1, 2, 5};

        for (int patLen = m; patLen <= n; ++patLen)

        {

            foreach (int start in startNums)

            {

                bool[] visited = new bool[10];

                visited[start] = true;

                // Optimization

                if (start != 5)

                {

                    numOfPatterns += this.NumberOfPatternsDFS(start, patLen - 1, visited, midNumMap) * 4;

                }

                else

                {

                    numOfPatterns += this.NumberOfPatternsDFS(start, patLen - 1, visited, midNumMap);

                }

            }

        }

        return numOfPatterns;

    }

    

    private int NumberOfPatternsDFS(int start, int remainingNums, bool[] visited, int[,] midNumMap)

    {

        if (remainingNums == 0)

        {

            return 1;

        }

        int numOfPatterns = 0;

        for (int nextNum = 1; nextNum <= 9; ++nextNum)

        {

            if (!visited[nextNum] && (midNumMap[start, nextNum] == 0 || visited[midNumMap[start, nextNum]]))

            {

                visited[nextNum] = true;

                numOfPatterns += this.NumberOfPatternsDFS(nextNum, remainingNums - 1, visited, midNumMap);

                visited[nextNum] = false;

            }

        }

        return numOfPatterns;

    }

    

    // Building a hash to check if there is a overlap between given two numbers

    private int[,] BuildMidNumMap()

    {

        int[,] midNumMap = new int[10, 10];

        midNumMap[1, 3] = 2;

        midNumMap[3, 1] = 2;

        midNumMap[3, 9] = 6;

        midNumMap[9, 3] = 6;

        midNumMap[9, 7] = 8;

        midNumMap[7, 9] = 8;

        midNumMap[7, 1] = 4;

        midNumMap[1, 7] = 4;

        midNumMap[1, 9] = 5;

        midNumMap[9, 1] = 5;

        midNumMap[3, 7] = 5;

        midNumMap[7, 3] = 5;

        midNumMap[2, 8] = 5;

        midNumMap[8, 2] = 5;

        midNumMap[4, 6] = 5;

        midNumMap[6, 4] = 5;

        return midNumMap;

    }


Complexity: (n - m) * !NumOfDigits

No comments:

Post a Comment