forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-beautiful-integers-in-the-range.py
138 lines (125 loc) · 5.76 KB
/
number-of-beautiful-integers-in-the-range.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
# Time: O(n^2 * k), n = len(str(high))
# Space: O(n^2 * k)
# memoization (faster but more space)
class Solution(object):
def numberOfBeautifulIntegers(self, low, high, k):
"""
:type low: int
:type high: int
:type k: int
:rtype: int
"""
TIGHT, UNTIGHT, UNBOUND = range(3)
def f(x):
digits = map(int, str(x))
lookup = [[[[-1]*k for _ in xrange(2*len(digits)+1)] for _ in xrange(3)] for _ in xrange(len(digits))]
def memoization(i, state, diff, total):
if i == len(digits):
return int(state != UNBOUND and diff == total == 0)
if lookup[i][state][diff][total] == -1:
result = int(i != 0 and diff == total == 0) # count if the beautiful integer x s.t. len(str(x)) < len(digits)
for d in xrange(1 if i == 0 else 0, 10):
new_state = state
if state == TIGHT and d != digits[i]:
new_state = UNTIGHT if d < digits[i] else UNBOUND
new_diff = diff+(1 if d%2 == 0 else -1)
new_total = (total*10+d)%k
result += memoization(i+1, new_state, new_diff, new_total)
lookup[i][state][diff][total] = result
return lookup[i][state][diff][total]
return memoization(0, TIGHT, 0, 0)
return f(high)-f(low-1)
# Time: O(n^2 * k), n = len(str(high))
# Space: O(n * k)
# dp (slower but less space)
class Solution2(object):
def numberOfBeautifulIntegers(self, low, high, k):
"""
:type low: int
:type high: int
:type k: int
:rtype: int
"""
TIGHT, UNTIGHT, UNBOUND = range(3)
def f(x):
digits = map(int, str(x))
dp = [[[0]*k for _ in xrange(2*len(digits)+1)] for _ in xrange(3)]
for tight in xrange(2):
for state in (TIGHT, UNTIGHT):
dp[state][0][0] = 1
for i in reversed(xrange(len(digits))):
new_dp = [[[0]*k for _ in xrange(2*len(digits)+1)] for _ in xrange(3)]
for state in (TIGHT, UNTIGHT, UNBOUND):
new_dp[state][0][0] = int(i != 0) # count if the beautiful integer x s.t. len(str(x)) < len(digits)
for d in xrange(1 if i == 0 else 0, 10):
new_state = state
if state == TIGHT and d != digits[i]:
new_state = UNTIGHT if d < digits[i] else UNBOUND
for diff in xrange(-len(digits), len(digits)+1):
new_diff = diff+(1 if d%2 == 0 else -1)
for total in xrange(k):
new_total = (total*10+d)%k
new_dp[state][diff][total] += dp[new_state][new_diff][new_total]
dp = new_dp
return dp[TIGHT][0][0]
return f(high)-f(low-1)
# Time: O(n^2 * k), n = len(str(high))
# Space: O(n^2 * k)
# memoization (faster but more space)
class Solution3(object):
def numberOfBeautifulIntegers(self, low, high, k):
"""
:type low: int
:type high: int
:type k: int
:rtype: int
"""
def f(x):
digits = map(int, str(x))
lookup = [[[[[-1]*k for _ in xrange(2*len(digits)+1)] for _ in xrange(2)] for _ in xrange(2)] for _ in xrange(len(digits))]
def memoization(i, zero, tight, diff, total):
if i == len(digits):
return int(zero == diff == total == 0)
if lookup[i][zero][tight][diff][total] == -1:
result = 0
for d in xrange((digits[i] if tight else 9)+1):
new_zero = int(zero and d == 0)
new_tight = int(tight and d == digits[i])
new_diff = diff+((1 if d%2 == 0 else -1) if new_zero == 0 else 0)
new_total = (total*10+d)%k
result += memoization(i+1, new_zero, new_tight, new_diff, new_total)
lookup[i][zero][tight][diff][total] = result
return lookup[i][zero][tight][diff][total]
return memoization(0, 1, 1, 0, 0)
return f(high)-f(low-1)
# Time: O(n^2 * k), n = len(str(high))
# Space: O(n * k)
# dp (slower but less space)
class Solution4(object):
def numberOfBeautifulIntegers(self, low, high, k):
"""
:type low: int
:type high: int
:type k: int
:rtype: int
"""
def f(x):
digits = map(int, str(x))
dp = [[[[0]*k for _ in xrange(2*len(digits)+1)] for _ in xrange(2)] for _ in xrange(2)]
for tight in xrange(2):
dp[0][tight][0][0] = 1
for i in reversed(xrange(len(digits))):
new_dp = [[[[0]*k for _ in xrange(2*len(digits)+1)] for _ in xrange(2)] for _ in xrange(2)]
for zero in xrange(2):
for tight in xrange(2):
for d in xrange((digits[i] if tight else 9)+1):
new_zero = int(zero and d == 0)
new_tight = int(tight and d == digits[i])
for diff in xrange(-len(digits), len(digits)+1):
new_diff = diff+((1 if d%2 == 0 else -1) if new_zero == 0 else 0)
for total in xrange(k):
new_total = (total*10+d)%k
new_dp[zero][tight][diff][total] += dp[new_zero][new_tight][new_diff][new_total]
dp = new_dp
return dp[1][1][0][0]
return f(high)-f(low-1)