{"id":5364,"date":"2019-07-27T22:01:54","date_gmt":"2019-07-28T05:01:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5364"},"modified":"2019-07-27T22:54:35","modified_gmt":"2019-07-28T05:54:35","slug":"leetcode-1139-largest-1-bordered-square","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1139-largest-1-bordered-square\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1139. Largest 1-Bordered Square"},"content":{"rendered":"\n<p>Given a 2D&nbsp;<code>grid<\/code>&nbsp;of&nbsp;<code>0<\/code>s and&nbsp;<code>1<\/code>s, return the number of elements in&nbsp;the largest&nbsp;<strong>square<\/strong>&nbsp;subgrid that has all&nbsp;<code>1<\/code>s on its&nbsp;<strong>border<\/strong>, or&nbsp;<code>0<\/code>&nbsp;if such a subgrid&nbsp;doesn&#8217;t exist in the&nbsp;<code>grid<\/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> grid = [[1,1,1],[1,0,1],[1,1,1]]\n<strong>Output:<\/strong> 9\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> grid = [[1,1,0,0]]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= grid.length &lt;= 100<\/code><\/li><li><code>1 &lt;= grid[0].length &lt;= 100<\/code><\/li><li><code>grid[i][j]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Compute the sums of all rectangles that has left-top corner at (0, 0) in O(m*n) time. <br>For each square and check whether its borders are all ones in O(1) time.<\/p>\n\n\n\n<p>Time complexity: O(m*n*min(m,n))<br>Space complexity: O(m*n)<\/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 largest1BorderedSquare(vector<vector<int>>& grid) {\n    int n = grid.size();\n    int m = grid[0].size();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1));\n    for (int i = 1; i <= n; ++i)\n      for (int j = 1; j <= m; ++j)\n        dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + grid[i - 1][j - 1];\n    \n    for (int len = min(n, m); len > 0; --len)\n      for (int x1 = 1, x2 = x1 + len - 1; x2 <= m; ++x1, ++x2)\n        for (int y1 = 1, y2 = y1 + len - 1; y2 <= n; ++y1, ++y2)\n          if (getArea(x1, y1, x2, y1, dp) == len \n              &#038;&#038; getArea(x1, y1, x1, y2, dp) == len\n              &#038;&#038; getArea(x1, y2, x2, y2, dp) == len\n              &#038;&#038; getArea(x2, y1, x2, y2, dp) == len)            \n            return len * len;        \n    return 0;\n  }\nprivate:\n  int getArea(int x1, int y1, int x2, int y2, const vector<vector<int>>& dp) {\n    return dp[y2][x2] - dp[y2][x1 - 1] - dp[y1 - 1][x2] + dp[y1 - 1][x1 - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a 2D&nbsp;grid&nbsp;of&nbsp;0s and&nbsp;1s, return the number of elements in&nbsp;the largest&nbsp;square&nbsp;subgrid that has all&nbsp;1s on its&nbsp;border, or&nbsp;0&nbsp;if such a subgrid&nbsp;doesn&#8217;t exist in the&nbsp;grid. Example 1:&#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,177],"class_list":["post-5364","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-2d","tag-dp","tag-grid","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5364","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=5364"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5364\/revisions"}],"predecessor-version":[{"id":5371,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5364\/revisions\/5371"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}