forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 15
/
android-unlock-patterns.py
188 lines (149 loc) · 5.1 KB
/
android-unlock-patterns.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Time: O(9^2 * 2^9)
# Space: O(9 * 2^9)
try:
xrange # Python 2
except NameError:
xrange = range # Python 3
# DP solution.
class Solution(object):
def numberOfPatterns(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def merge(used, i):
return used | (1 << i)
def number_of_keys(i):
number = 0
while i > 0:
i &= i - 1
number += 1
return number
def contain(used, i):
return bool(used & (1 << i))
def convert(i, j):
return 3 * i + j
# dp[i][j]: i is the set of the numbers in binary representation,
# dp[i][j] is the number of ways ending with the number j.
dp = [[0] * 9 for _ in xrange(1 << 9)]
for i in xrange(9):
dp[merge(0, i)][i] = 1
res = 0
for used in xrange(len(dp)):
number = number_of_keys(used)
if number > n:
continue
for i in xrange(9):
if not contain(used, i):
continue
if m <= number <= n:
res += dp[used][i]
x1, y1 = divmod(i, 3)
for j in xrange(9):
if contain(used, j):
continue
x2, y2 = divmod(j, 3)
if ((x1 == x2 and abs(y1 - y2) == 2) or
(y1 == y2 and abs(x1 - x2) == 2) or
(abs(x1 - x2) == 2 and abs(y1 - y2) == 2)) and \
not contain(used,
convert((x1 + x2) // 2, (y1 + y2) // 2)):
continue
dp[merge(used, j)][j] += dp[used][i]
return res
# Time: O(9^2 * 2^9)
# Space: O(9 * 2^9)
# DP solution.
class Solution2(object):
def numberOfPatterns(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def merge(used, i):
return used | (1 << i)
def number_of_keys(i):
number = 0
while i > 0:
i &= i - 1
number += 1
return number
def exclude(used, i):
return used & ~(1 << i)
def contain(used, i):
return bool(used & (1 << i))
def convert(i, j):
return 3 * i + j
# dp[i][j]: i is the set of the numbers in binary representation,
# d[i][j] is the number of ways ending with the number j.
dp = [[0] * 9 for _ in xrange(1 << 9)]
for i in xrange(9):
dp[merge(0, i)][i] = 1
res = 0
for used in xrange(len(dp)):
number = number_of_keys(used)
if number > n:
continue
for i in xrange(9):
if not contain(used, i):
continue
x1, y1 = divmod(i, 3)
for j in xrange(9):
if i == j or not contain(used, j):
continue
x2, y2 = divmod(j, 3)
if ((x1 == x2 and abs(y1 - y2) == 2) or
(y1 == y2 and abs(x1 - x2) == 2) or
(abs(x1 - x2) == 2 and abs(y1 - y2) == 2)) and \
not contain(used,
convert((x1 + x2) // 2, (y1 + y2) // 2)):
continue
dp[used][i] += dp[exclude(used, i)][j]
if m <= number <= n:
res += dp[used][i]
return res
# Time: O(9!)
# Space: O(9)
# Backtracking solution. (TLE)
class Solution_TLE(object):
def numberOfPatterns(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def merge(used, i):
return used | (1 << i)
def contain(used, i):
return bool(used & (1 << i))
def convert(i, j):
return 3 * i + j
def numberOfPatternsHelper(m, n, level, used, i):
number = 0
if level > n:
return number
if m <= level <= n:
number += 1
x1, y1 = divmod(i, 3)
for j in xrange(9):
if contain(used, j):
continue
x2, y2 = divmod(j, 3)
if ((x1 == x2 and abs(y1 - y2) == 2) or
(y1 == y2 and abs(x1 - x2) == 2) or
(abs(x1 - x2) == 2 and abs(y1 - y2) == 2)) and \
not contain(used,
convert((x1 + x2) // 2, (y1 + y2) // 2)):
continue
number += numberOfPatternsHelper(m, n, level + 1, merge(used, j), j)
return number
number = 0
# 1, 3, 7, 9
number += 4 * numberOfPatternsHelper(m, n, 1, merge(0, 0), 0)
# 2, 4, 6, 8
number += 4 * numberOfPatternsHelper(m, n, 1, merge(0, 1), 1)
# 5
number += numberOfPatternsHelper(m, n, 1, merge(0, 4), 4)
return number