{"id":6638,"date":"2020-04-18T13:20:41","date_gmt":"2020-04-18T20:20:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6638"},"modified":"2020-04-18T19:28:39","modified_gmt":"2020-04-19T02:28:39","slug":"leetcode-1416-restore-the-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1416-restore-the-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1416. Restore The Array"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1416. Restore The Array - \u5237\u9898\u627e\u5de5\u4f5c EP320\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/mdUTRI2FMtU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range&nbsp;<code>[1, k]<\/code>&nbsp;and there are no leading zeros in the array.<\/p>\n\n\n\n<p>Given the string&nbsp;<code>s<\/code>&nbsp;and the integer&nbsp;<code>k<\/code>. There can be multiple ways to restore the array.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of possible array<\/em>&nbsp;that can be printed as a string&nbsp;<code>s<\/code>&nbsp;using the mentioned program.<\/p>\n\n\n\n<p>The number of ways could be very large so return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10^9 + 7<\/code><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1000\", k = 10000\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The only possible array is [1000]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1000\", k = 10\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There cannot be an array that was printed this way and has all integer &gt;= 1 and &lt;= 10.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1317\", k = 2000\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong> Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"2020\", k = 30\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The only possible array is [20,20]. [2020] is invalid because 2020 &gt; 30. [2,020] is ivalid because 020 contains leading zeros.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1234567890\", k = 90\n<strong>Output:<\/strong> 34\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10^5<\/code>.<\/li><li><code>s<\/code>&nbsp;consists of only digits and doesn&#8217;t contain leading zeros.<\/li><li><code>1 &lt;= k &lt;= 10^9<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i] := # of ways to restore the array for s[i:n].<\/p>\n\n\n\n<p>dp[i] = sum(dp[j]), where 0 &lt; j &lt; n, int(s[i:j]) &lt;= k, s[i] != 0<\/p>\n\n\n\n<p>Time complexity: O(n*logk)<br>Space complexity: O(n)<\/p>\n\n\n\n<p>Top-down<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numberOfArrays(string s, int k) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = s.length();\n    vector<int> mem(n, -1);\n    \/\/ dp(i) returns # of ways for s[i:n]\n    function<int(int)> dp = [&](int i) {\n      if (i == n) return 1;\n      if (s[i] == '0') return 0;\n      if (mem[i] >= 0) return mem[i];\n      long num = 0;\n      int ans = 0;\n      for (int j = i + 1; j <= n; ++j) {\n        num = num * 10 + (s[j - 1] - '0');\n        if (num > k) break;\n        ans = (ans + dp(j)) % kMod;\n      }\n      return mem[i] = ans;\n    };\n    return dp(0);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>bottom-up<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numberOfArrays(string s, int k) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = s.length();\n    vector<int> dp(n + 1); \/\/ # of ways for s[i:n]\n    dp.back() = 1;\n    for (int i = n - 1; i >= 0; --i) {\n      if (s[i] == '0') continue;\n      long num = 0;\n      for (int j = i + 1; j <= n; ++j) {\n        num = num * 10 + (s[j - 1] - '0');\n        if (num > k) break;\n        dp[i] = (dp[i] + dp[j]) % kMod;\n      }\n    }\n    return dp[0];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,276,4],"class_list":["post-6638","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-split","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6638"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6638\/revisions"}],"predecessor-version":[{"id":6642,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6638\/revisions\/6642"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}