Issues (64)

src/leetcode/AndroidUnlockPatterns.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class AndroidUnlockPatterns
8
{
9
    public static function numberOfPatterns(int $m, int $n): int
10
    {
11
        $ans = 0;
12
        if ($m <= 0 || $m > 9 || $n <= 0 || $n > 9) {
13
            return $ans;
14
        }
15
        $visited = array_fill(0, 10, false);
16
        $jumps = array_fill(0, 10, array_fill(0, 10, 0));
17
        $jumps[1][3] = $jumps[3][1] = 2;
18
        $jumps[1][7] = $jumps[7][1] = 4;
19
        $jumps[3][9] = $jumps[9][3] = 6;
20
        $jumps[7][9] = $jumps[9][7] = 8;
21
        $jumps[4][6] = $jumps[6][4] = $jumps[2][8] = $jumps[8][2] = 5;
22
        $jumps[1][9] = $jumps[9][1] = $jumps[3][7] = $jumps[7][3] = 5;
23
        $ans += self::dfs(1, 1, $m, $n, $jumps, $visited, 0) * 4;
24
        $ans += self::dfs(2, 1, $m, $n, $jumps, $visited, 0) * 4;
25
        $ans += self::dfs(5, 1, $m, $n, $jumps, $visited, 0);
26
27
        return $ans;
28
    }
29
30
    public static function numberOfPatterns2(int $m, int $n): int
31
    {
32
        $ans = 0;
33
        if ($m <= 0 || $m > 9 || $n <= 0 || $n > 9) {
34
            return $ans;
35
        }
36
        $visited = array_fill(0, 10, false);
37
        $jumps[1][3] = $jumps[3][1] = 2;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$jumps was never initialized. Although not strictly required by PHP, it is generally a good practice to add $jumps = array(); before regardless.
Loading history...
38
        $jumps[1][7] = $jumps[7][1] = 4;
39
        $jumps[7][9] = $jumps[9][7] = 8;
40
        $jumps[3][9] = $jumps[9][3] = 6;
41
        $jumps[4][6] = $jumps[6][4] = $jumps[2][8] = $jumps[8][2] = 5;
42
        $jumps[1][9] = $jumps[9][1] = $jumps[3][7] = $jumps[7][3] = 5;
43
        for ($i = $m; $i <= $n; $i++) {
44
            $ans += self::dfs2(1, $jumps, $visited, $i - 1) * 4;
45
            $ans += self::dfs2(2, $jumps, $visited, $i - 1) * 4;
46
            $ans += self::dfs2(5, $jumps, $visited, $i - 1);
47
        }
48
49
        return $ans;
50
    }
51
52
    private static function dfs(
53
        int $curr,
54
        int $len,
55
        int $m,
56
        int $n,
57
        array &$jumps,
58
        array &$visited,
59
        int $ans
60
    ) {
61
        if ($len >= $m) {
62
            $ans++;
63
        }
64
        $len++;
65
        if ($len > $n) {
66
            return $ans;
67
        }
68
        $visited[$curr] = true;
69
        for ($next = 1; $next < 10; $next++) {
70
            $jump = $jumps[$curr][$next];
71
            if (!$visited[$next] && ($jump === 0 || $visited[$jump])) {
72
                $ans = self::dfs($next, $len, $m, $n, $jumps, $visited, $ans);
73
            }
74
        }
75
        $visited[$curr] = false;
76
77
        return $ans;
78
    }
79
80
    private static function dfs2(
81
        int $curr,
82
        array &$jumps,
83
        array &$visited,
84
        int $remain
85
    ) {
86
        if ($remain === 0) {
87
            return 1;
88
        }
89
        $ans = 0;
90
        $visited[$curr] = true;
91
        for ($next = 1; $next < 10; $next++) {
92
            $jump = $jumps[$curr][$next];
93
            if (!$visited[$next] && ($jump === 0 || $visited[$jump])) {
94
                $ans += self::dfs2($next, $jumps, $visited, $remain - 1);
95
            }
96
        }
97
        $visited[$curr] = false;
98
99
        return $ans;
100
    }
101
}
102