Skip to content

Commit aadf672

Browse files
authored
Create restore-the-array.cpp
1 parent 374ab8f commit aadf672

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

C++/restore-the-array.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time: O(nlogk)
2+
// Space: O(logk)
3+
4+
class Solution {
5+
public:
6+
int numberOfArrays(string s, int k) {
7+
static const int MOD = 1e9 + 7;
8+
int klen = to_string(k).length();
9+
vector<int> dp(klen + 1);
10+
dp[s.length() % dp.size()] = 1;
11+
for (int i = s.length() - 1; i >= 0; --i) {
12+
dp[i % dp.size()] = 0;
13+
if (s[i] == '0') {
14+
continue;
15+
}
16+
for (uint64_t j = i, curr = 0; j < min(uint64_t(i + klen), s.length()); ++j) {
17+
curr = curr * 10 + s[j] - '0';
18+
if (curr > k) {
19+
break;
20+
}
21+
dp[i % dp.size()] = (dp[i % dp.size()] + dp[(j + 1) % dp.size()]) % MOD;
22+
}
23+
}
24+
return dp[0];
25+
}
26+
};

0 commit comments

Comments
 (0)