aboutsummaryrefslogtreecommitdiff
path: root/other/restore_array.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/restore_array.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/restore_array.cpp')
-rw-r--r--other/restore_array.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/other/restore_array.cpp b/other/restore_array.cpp
new file mode 100644
index 0000000..4f4a59a
--- /dev/null
+++ b/other/restore_array.cpp
@@ -0,0 +1,37 @@
+// 1416. Restore The Array
+#include "leetcode.h"
+
+/*
+ * a progamme was supposed to print an array of integers. the programme forgot
+ * to print whitespace and the array is printing as a string of digits 's' and
+ * all we know is that all integers in the array were in the range [1, k] and
+ * there are no leading zeros in the array. given the string 's' and the integer
+ * 'k', return the number of possible arrays that can be printed as 's' using
+ * the mentioned programme. return modulo 10^9+7
+ */
+
+class Solution {
+public:
+ int numberOfArrays(string s, int k) {
+ vector<int> dp(s.size() + 1);
+ dp[s.size()] = 1;
+ for (int i = s.size() - 1; i >= 0; --i) {
+ if (s[i] == '0')
+ continue;
+ for (long sz = 1, n = 0; i + sz <= s.size(); ++sz) {
+ n = n * 10 + s[i + sz - 1] - '0';
+ if (n > k)
+ break;
+ dp[i] = (dp[i] + dp[i + sz]) % 1000000007;
+ }
+ }
+ return dp[0];
+ }
+};
+
+int main() {
+ Solution obj;
+ printf("%d\n", obj.numberOfArrays("1000", 10000)); // expect: 1
+ printf("%d\n", obj.numberOfArrays("1000", 10)); // expect: 0
+ printf("%d\n", obj.numberOfArrays("1317", 2000)); // expect: 8
+}