We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 374ab8f commit aadf672Copy full SHA for aadf672
C++/restore-the-array.cpp
@@ -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