{"id":5912,"date":"2019-12-01T23:21:33","date_gmt":"2019-12-02T07:21:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5912"},"modified":"2019-12-01T23:21:52","modified_gmt":"2019-12-02T07:21:52","slug":"leetcode-1277-count-square-submatrices-with-all-ones","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1277-count-square-submatrices-with-all-ones\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1277. Count Square Submatrices with All Ones"},"content":{"rendered":"\n<p>Given a&nbsp;<code>m * n<\/code>&nbsp;matrix of ones and zeros, return how many&nbsp;<strong>square<\/strong>&nbsp;submatrices have all ones.<\/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> matrix =\n[\n&nbsp; [0,1,1,1],\n&nbsp; [1,1,1,1],\n&nbsp; [0,1,1,1]\n]\n<strong>Output:<\/strong> 15\n<strong>Explanation:<\/strong> \nThere are <strong>10<\/strong> squares of side 1.\nThere are <strong>4<\/strong> squares of side 2.\nThere is  <strong>1<\/strong> square of side 3.\nTotal number of squares = 10 + 4 + 1 = <strong>15<\/strong>.\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> matrix = \n[\n  [1,0,1],\n  [1,1,0],\n  [1,1,0]\n]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> \nThere are <strong>6<\/strong> squares of side 1.  \nThere is <strong>1<\/strong> square of side 2. \nTotal number of squares = 6 + 1 = <strong>7<\/strong>.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length&nbsp;&lt;= 300<\/code><\/li><li><code>1 &lt;= arr[0].length&nbsp;&lt;= 300<\/code><\/li><li><code>0 &lt;= arr[i][j] &lt;= 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i][j] := edge of largest square with bottom right corner at (i, j)<br>dp[i][j] = min(dp[i &#8211; 1][j], dp[i &#8211; 1][j &#8211; 1], dp[i][j &#8211; 1]) if m[i][j] == 1 else 0<br>ans = sum(dp)<\/p>\n\n\n\n<p>Time complexity: O(n*m)<br>Space complexity: O(n*m)<\/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 countSquares(vector<vector<int>>& matrix) {\n    const int n = matrix.size();\n    const int m = matrix[0].size();\n    \/\/ dp[i][j] := edge of largest square with right bottom corner at (i, j)\n    vector<vector<int>> dp(n, vector<int>(m));\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      for (int j = 0; j < m; ++j) {\n        dp[i][j] = matrix[i][j];\n        if (i &#038;&#038; j &#038;&#038; dp[i][j])\n          dp[i][j] = min({dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]}) + 1;\n        ans += dp[i][j];\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;m * n&nbsp;matrix of ones and zeros, return how many&nbsp;square&nbsp;submatrices have all ones. Example 1: Input: matrix = [ &nbsp; [0,1,1,1], &nbsp; [1,1,1,1], &nbsp;&#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,278,177,519,285],"class_list":["post-5912","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-grid","tag-medium","tag-ones","tag-square","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5912","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=5912"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5912\/revisions"}],"predecessor-version":[{"id":5914,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5912\/revisions\/5914"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}