-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcount-good-integers-on-a-grid-path.cpp
More file actions
238 lines (224 loc) · 7.39 KB
/
count-good-integers-on-a-grid-path.cpp
File metadata and controls
238 lines (224 loc) · 7.39 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Time: O(16 * 2 * 10 * 10)
// Space: O(16 + 2 * 10)
// dp
class Solution {
public:
long long countGoodIntegersOnPath(long long l, long long r, string directions) {
static const int L = 16;
vector<bool> lookup(L);
const auto& count = [&](int64_t n) {
vector<int> digits(L);
for (int i = L - 1; i >= 0; --i) {
digits[i] = n % 10;
n /= 10;
}
int64_t dp[2][10] = {};
dp[1][0] = 1;
for (int i = 0; i < L; ++i) {
int64_t new_dp[2][10] = {};
for (int t = 0; t < 2; ++t) {
const auto& bound = t ? digits[i] : 9;
for (int k = 0; k < 10; ++k) {
if (!dp[t][k]) {
continue;
}
for (int d = 0; d <= bound; ++d) {
int nk = k;
if (lookup[i]) {
if (d < k) {
continue;
}
nk = d;
}
new_dp[t && d == bound][nk] += dp[t][k];
}
}
}
for (int t = 0; t < 2; ++t) {
for (int k = 0; k < 10; ++k) {
dp[t][k] = new_dp[t][k];
}
}
}
int64_t result = 0;
for (int t = 0; t < 2; ++t) {
for (int k = 0; k < 10; ++k) {
result += dp[t][k];
}
}
return result;
};
int i = 0, j = 0;
lookup[i * 4 + j] = true;
for (const auto& x : directions) {
if (x == 'D') {
++i;
} else {
++j;
}
lookup[i * 4 + j] = true;
}
return count(r) - count(l - 1);
}
};
// Time: O(16 * 2 * 10 * 10)
// Space: O(16 + 2 * 10)
// dp
class Solution2 {
public:
long long countGoodIntegersOnPath(long long l, long long r, string directions) {
static const int L = 16;
vector<bool> lookup(L);
const auto& count = [&](int64_t n) {
vector<int> digits(L);
for (int i = L - 1; i >= 0; --i) {
digits[i] = n % 10;
n /= 10;
}
vector<vector<int64_t>> dp(2, vector<int64_t>(10));
dp[1][0] = 1;
for (int i = 0; i < L; ++i) {
vector<vector<int64_t>> new_dp(2, vector<int64_t>(10));
for (int t = 0; t < 2; ++t) {
const auto& bound = t ? digits[i] : 9;
for (int k = 0; k < 10; ++k) {
if (!dp[t][k]) {
continue;
}
for (int d = 0; d <= bound; ++d) {
int nk = k;
if (lookup[i]) {
if (d < k) {
continue;
}
nk = d;
}
new_dp[t && d == bound][nk] += dp[t][k];
}
}
}
dp = move(new_dp);
}
int64_t result = 0;
for (const auto& row : dp) {
result += accumulate(cbegin(row), cend(row), 0LL);
}
return result;
};
int i = 0, j = 0;
lookup[i * 4 + j] = true;
for (const auto& x : directions) {
if (x == 'D') {
++i;
} else {
++j;
}
lookup[i * 4 + j] = true;
}
return count(r) - count(l - 1);
}
};
// Time: O(16 * 2 * 10 * 10)
// Space: O(16 * 10)
// memoization
class Solution3 {
public:
long long countGoodIntegersOnPath(long long l, long long r, string directions) {
static const int L = 16;
const auto& count = [&](int64_t n) {
vector<bool> lookup(L);
vector<int> digits(L);
vector<vector<int64_t>> memo(L, vector<int64_t>(10, -1));
const auto memoization = [&](this auto&& memoization, int i, bool t, int k) -> int64_t {
if (i == 16) {
return 1;
}
if (!t && memo[i][k] != -1) {
return memo[i][k];
}
int64_t result = 0;
const auto& bound = t ? digits[i] : 9;
for (int d = 0; d <= bound; ++d) {
int nk = k;
if (lookup[i]) {
if (d < k) {
continue;
}
nk = d;
}
result += memoization(i + 1, t && (d == bound), nk);
}
if (!t) {
memo[i][k] = result;
}
return result;
};
for (int i = L - 1; i >= 0; --i) {
digits[i] = n % 10;
n /= 10;
}
int i = 0, j = 0;
lookup[i * 4 + j] = true;
for (const auto& x : directions) {
if (x == 'D') {
++i;
} else {
++j;
}
lookup[i * 4 + j] = true;
}
return memoization(0, true, 0);
};
return count(r) - count(l - 1);
}
};
// Time: O(16 * 2 * 10 * 10)
// Space: O(16 * 2 * 10)
// memoization
class Solution4 {
public:
long long countGoodIntegersOnPath(long long l, long long r, string directions) {
static const int L = 16;
const auto& count = [&](int64_t n) {
vector<bool> lookup(L);
vector<int> digits(L);
vector<vector<vector<int64_t>>> memo(L, vector<vector<int64_t>>(2, vector<int64_t>(10, -1)));
const auto memoization = [&](this auto&& memoization, int i, bool t, int k) -> int64_t {
if (i == 16) {
return 1;
}
if (memo[i][t][k] == -1) {
memo[i][t][k] = 0;
const auto& bound = t ? digits[i] : 9;
for (int d = 0; d <= bound; ++d) {
int nk = k;
if (lookup[i]) {
if (d < k) {
continue;
}
nk = d;
}
memo[i][t][k] += memoization(i + 1, t && (d == bound), nk);
}
}
return memo[i][t][k];
};
for (int i = L - 1; i >= 0; --i) {
digits[i] = n % 10;
n /= 10;
}
int i = 0, j = 0;
lookup[i * 4 + j] = true;
for (const auto& x : directions) {
if (x == 'D') {
++i;
} else {
++j;
}
lookup[i * 4 + j] = true;
}
return memoization(0, true, 0);
};
return count(r) - count(l - 1);
}
};