-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0351-android-unlock-patterns.js
More file actions
59 lines (48 loc) · 2.07 KB
/
0351-android-unlock-patterns.js
File metadata and controls
59 lines (48 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Android Unlock Patterns
* Time Complexity: O(N_dots * P(N_dots, n))
* Space Complexity: O(n)
*/
var numberOfPatterns = function (m, n) {
const skipMatrix = new Array(10).fill(null).map(() => new Array(10).fill(0));
skipMatrix[1][3] = skipMatrix[3][1] = 2;
skipMatrix[1][7] = skipMatrix[7][1] = 4;
skipMatrix[3][9] = skipMatrix[9][3] = 6;
skipMatrix[7][9] = skipMatrix[9][7] = 8;
skipMatrix[1][9] = skipMatrix[9][1] = skipMatrix[2][8] = skipMatrix[8][2] = skipMatrix[3][7] = skipMatrix[7][3] = skipMatrix[4][6] = skipMatrix[6][4] = 5;
const recursiveSolver = (currentPosition, patternVisited, patternLength) => {
if (patternLength > n) {
return 0;
}
let currentPatternCount = 0;
if (patternLength >= m) {
currentPatternCount = 1;
}
if (patternLength === n) {
return currentPatternCount;
}
for (let potentialNext = 1; potentialNext <= 9; potentialNext++) {
if (patternVisited.has(potentialNext) === false) {
const passedThroughKey = skipMatrix[currentPosition][potentialNext];
if (passedThroughKey === 0 || patternVisited.has(passedThroughKey)) {
patternVisited.add(potentialNext);
currentPatternCount += recursiveSolver(potentialNext, patternVisited, patternLength + 1);
patternVisited.delete(potentialNext);
}
}
}
return currentPatternCount;
};
let totalValidPatterns = 0;
const initialVisitedSet = new Set();
const startPoints = [1, 2, 5];
const symmetryMultipliers = [4, 4, 1];
for (let indexValue = 0; indexValue < startPoints.length; indexValue++) {
const starterDot = startPoints[indexValue];
const multiplierFactor = symmetryMultipliers[indexValue];
initialVisitedSet.add(starterDot);
totalValidPatterns += recursiveSolver(starterDot, initialVisitedSet, 1) * multiplierFactor;
initialVisitedSet.delete(starterDot);
}
return totalValidPatterns;
};