-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcount-beautiful-numbers.py
33 lines (29 loc) · 1.04 KB
/
count-beautiful-numbers.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
# Time: O(logr * 2 * 10 * s)
# Space: O(s) ~= O(2026), s = len(states)
import collections
# dp
class Solution(object):
def beautifulNumbers(self, l, r):
"""
:type l: int
:type r: int
:rtype: int
"""
def count(x):
s = map(lambda x: ord(x)-ord('0'), str(x))
dp = [collections.defaultdict(int) for _ in xrange(2)]
dp[1][1, 0] = 1
for c in s:
new_dp = [collections.defaultdict(int) for _ in xrange(2)]
for b in xrange(2):
for (mul, total), cnt in dp[b].iteritems():
for x in xrange((c if b else 9)+1):
new_dp[b and x == c][mul*(1 if total == 0 == x else x), total+x] += cnt
dp = new_dp
result = 0
for b in xrange(2):
for (mul, total), cnt in dp[b].iteritems():
if total and mul%total == 0:
result += cnt
return result
return count(r)-count(l-1)