{"id":5201,"date":"2019-05-17T21:58:48","date_gmt":"2019-05-18T04:58:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5201"},"modified":"2019-05-17T23:33:52","modified_gmt":"2019-05-18T06:33:52","slug":"leetcode-718-maximum-length-of-repeated-subarray","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-718-maximum-length-of-repeated-subarray\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 718. Maximum Length of Repeated Subarray"},"content":{"rendered":"\n<p>iven two integer arrays&nbsp;<code>A<\/code>&nbsp;and&nbsp;<code>B<\/code>, return the maximum length of an subarray that appears in both arrays.<\/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>\nA: [1,2,3,2,1]\nB: [3,2,1,4,7]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \nThe repeated subarray with maximum length is [3, 2, 1].\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>1 &lt;= len(A), len(B) &lt;= 1000<\/li><li>0 &lt;= A[i], B[i] &lt; 100<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i][j] := max length of (A[0:i], B[0:j])<\/p>\n\n\n\n<p>dp[i][j] = dp[i &#8211; 1][j &#8211; 1] + 1 if A[i-1] == B[j-1] else 0<\/p>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(m*n) -&gt; O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++ S:O(mn)<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 176 ms \/ 106.4 MB\nclass Solution {\npublic:\n  int findLength(vector<int>& A, vector<int>& B) {\n    int m = A.size();\n    int n = B.size();\n    vector<vector<int>> dp(m + 1, vector<int>(n + 1));    \n    int ans = 0;\n    for (int i = 1; i <= m; ++i)\n      for (int j = 1; j <= n; ++j)\n        if (A[i - 1] == B[j - 1]) {\n          dp[i][j] = dp[i - 1][j - 1] + 1;\n          ans = max(ans, dp[i][j]);\n        }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ S:O(min(m,n))<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 152 ms \/ 9.1 MB\nclass Solution {\npublic:\n  int findLength(vector<int>& A, vector<int>& B) {\n    if (A.size() < B.size()) swap(A, B);\n    int m = A.size();\n    int n = B.size();\n    vector<int> dp(n + 1);    \n    int ans = 0;\n    for (int i = 1; i <= m; ++i)\n      for (int j = n; j >= 1; --j) {\n        dp[j] = A[i - 1] == B[j - 1] ? dp[j - 1] + 1 : 0;        \n        ans = max(ans, dp[j]);\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>iven two integer arrays&nbsp;A&nbsp;and&nbsp;B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3&#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,177,41],"class_list":["post-5201","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5201","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=5201"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5201\/revisions"}],"predecessor-version":[{"id":5205,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5201\/revisions\/5205"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}