{"id":4686,"date":"2019-01-26T10:21:08","date_gmt":"2019-01-26T18:21:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4686"},"modified":"2019-01-26T10:22:34","modified_gmt":"2019-01-26T18:22:34","slug":"leetcode-85-maximal-rectangle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-85-maximal-rectangle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 85. Maximal Rectangle"},"content":{"rendered":"\n<p>Given a 2D binary matrix filled with 0&#8217;s and 1&#8217;s, find the largest rectangle containing only 1&#8217;s and return its area.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input:<\/strong>\n[\n  [\"1\",\"0\",\"1\",\"0\",\"0\"],\n  [\"1\",\"0\",\"<strong>1<\/strong>\",\"<strong>1<\/strong>\",\"<strong>1<\/strong>\"],\n  [\"1\",\"1\",\"<strong>1<\/strong>\",\"<strong>1<\/strong>\",\"<strong>1<\/strong>\"],\n  [\"1\",\"0\",\"0\",\"1\",\"0\"]\n]\n<strong>Output:<\/strong> 6<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: DP<\/h2>\n\n\n\n<p>Time complexity: O(m^2*n)<br>Space complexity: O(mn)<\/p>\n\n\n\n<p>dp[i][j] := max length of all 1 sequence ends with col j, at the i-th row.<br>transition: <br>dp[i][j] = 0 if matrix[i][j] == &#8216;0&#8217;<br>             = dp[i][j-1] + 1 if  matrix[i][j] == &#8216;1&#8217; <\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int maximalRectangle(vector<vector<char> > &matrix) {\n    int r = matrix.size();\n    if (r == 0) return 0;\n    int c = matrix[0].size();\n  \n    \/\/ dp[i][j] := max len of all 1s ends with col j at row i.\n    vector<vector<int>> dp(r, vector<int>(c));\n\n    for (int i = 0; i < r; ++i)\n      for (int j = 0; j < c; ++j)\n        dp[i][j] = (matrix[i][j] == '1') ? (j == 0 ? 1 : dp[i][j - 1] + 1) : 0;\n\n    int ans = 0;\n\n    for (int i = 0; i < r; ++i)\n      for (int j = 0; j < c; ++j) {\n        int len = INT_MAX;\n        for (int k = i; k < r; k++) {\n          len = min(len, dp[k][j]);\n          if (len == 0) break;\n          ans = max(len * (k - i + 1), ans);\n        }\n    }\n\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a 2D binary matrix filled with 0&#8217;s and 1&#8217;s, find the largest rectangle containing only 1&#8217;s and return its area. Example: Input: [ [&#8220;1&#8243;,&#8221;0&#8243;,&#8221;1&#8243;,&#8221;0&#8243;,&#8221;0&#8221;],&#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":[453,18,278,217,343],"class_list":["post-4686","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-2d","tag-dp","tag-grid","tag-hard","tag-rectangle","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4686","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=4686"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4686\/revisions"}],"predecessor-version":[{"id":4690,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4686\/revisions\/4690"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}