-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcount-good-integers-on-a-grid-path.py
More file actions
144 lines (134 loc) · 4.18 KB
/
count-good-integers-on-a-grid-path.py
File metadata and controls
144 lines (134 loc) · 4.18 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
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
# Time: O(16 * 2 * 10 * 10)
# Space: O(16 + 2 * 10)
# dp
class Solution(object):
def countGoodIntegersOnPath(self, l, r, directions):
"""
:type l: int
:type r: int
:type directions: str
:rtype: int
"""
L = 16
def count(n):
digits = [0]*L
for i in reversed(xrange(len(digits))):
digits[i] = n%10
n //= 10
dp = [[0]*10 for _ in xrange(2)]
dp[1][0] = 1
for i in xrange(L):
new_dp = [[0]*10 for _ in xrange(2)]
for t in xrange(2):
bound = digits[i] if t else 9
for k in xrange(10):
if not dp[t][k]:
continue
for d in xrange(bound+1):
nk = k
if lookup[i]:
if d < k:
continue
nk = d
new_dp[t and d == bound][nk] += dp[t][k]
dp = new_dp
return sum(sum(row) for row in dp)
i = j = 0
lookup = [False]*L
lookup[i*4+j] = True
for x in directions:
if x == 'D':
i += 1
else:
j += 1
lookup[i*4+j] = True
return count(r)-count(l-1)
# Time: O(16 * 2 * 10 * 10)
# Space: O(16 * 10)
# memoization
class Solution2(object):
def countGoodIntegersOnPath(self, l, r, directions):
"""
:type l: int
:type r: int
:type directions: str
:rtype: int
"""
L = 16
def count(n):
def memoization(i, t, k):
if i == L:
return 1
if not t and memo[i][k] != -1:
return memo[i][k]
result = 0
bound = digits[i] if t else 9
for d in xrange(bound+1):
nk = k
if lookup[i]:
if d < k:
continue
nk = d
result += memoization(i+1, t and d == bound, nk)
if not t:
memo[i][k] = result
return result
digits = [0]*L
for i in reversed(xrange(len(digits))):
digits[i] = n%10
n //= 10
memo = [[-1]*10 for _ in xrange(L)]
return memoization(0, True, 0)
i = j = 0
lookup = [False]*L
lookup[i*4+j] = True
for x in directions:
if x == 'D':
i += 1
else:
j += 1
lookup[i*4+j] = True
return count(r)-count(l-1)
# Time: O(16 * 2 * 10 * 10)
# Space: O(16 * 2 * 10)
# memoization
class Solution3(object):
def countGoodIntegersOnPath(self, l, r, directions):
"""
:type l: int
:type r: int
:type directions: str
:rtype: int
"""
L = 16
def count(n):
def memoization(i, t, k):
if i == L:
return 1
if memo[i][t][k] == -1:
memo[i][t][k] = 0
bound = digits[i] if t else 9
for d in xrange(bound+1):
nk = k
if lookup[i]:
if d < k:
continue
nk = d
memo[i][t][k] += memoization(i+1, t and d == bound, nk)
return memo[i][t][k]
digits = [0]*L
for i in reversed(xrange(len(digits))):
digits[i] = n%10
n //= 10
memo = [[[-1]*10 for _ in xrange(2)] for _ in xrange(L)]
return memoization(0, True, 0)
i = j = 0
lookup = [False]*L
lookup[i*4+j] = True
for x in directions:
if x == 'D':
i += 1
else:
j += 1
lookup[i*4+j] = True
return count(r)-count(l-1)