{"id":7031,"date":"2020-07-05T13:48:28","date_gmt":"2020-07-05T20:48:28","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7031"},"modified":"2020-07-05T14:28:41","modified_gmt":"2020-07-05T21:28:41","slug":"leetcode-1504-count-submatrices-with-all-ones","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1504-count-submatrices-with-all-ones\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1504. Count Submatrices With All Ones"},"content":{"rendered":"\n<p>Given a&nbsp;<code>rows * columns<\/code>&nbsp;matrix&nbsp;<code>mat<\/code>&nbsp;of ones and zeros, return how many&nbsp;<strong>submatrices<\/strong>&nbsp;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> mat = [[1,0,1],\n&nbsp;             [1,1,0],\n&nbsp;             [1,1,0]]\n<strong>Output:<\/strong> 13\n<strong>Explanation:\n<\/strong>There are <strong>6<\/strong> rectangles of side 1x1.\nThere are <strong>2<\/strong> rectangles of side 1x2.\nThere are <strong>3<\/strong> rectangles of side 2x1.\nThere is <strong>1<\/strong> rectangle of side 2x2. \nThere is <strong>1<\/strong> rectangle of side 3x1.\nTotal number of rectangles = 6 + 2 + 3 + 1 + 1 = <strong>13.<\/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> mat = [[0,1,1,0],\n&nbsp;             [0,1,1,1],\n&nbsp;             [1,1,1,0]]\n<strong>Output:<\/strong> 24\n<strong>Explanation:<\/strong>\nThere are <strong>8<\/strong> rectangles of side 1x1.\nThere are <strong>5<\/strong> rectangles of side 1x2.\nThere are <strong>2<\/strong> rectangles of side 1x3. \nThere are <strong>4<\/strong> rectangles of side 2x1.\nThere are <strong>2<\/strong> rectangles of side 2x2. \nThere are <strong>2<\/strong> rectangles of side 3x1. \nThere is <strong>1<\/strong> rectangle of side 3x2. \nTotal number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24<strong>.<\/strong>\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> mat = [[1,1,1,1,1,1]]\n<strong>Output:<\/strong> 21\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> mat = [[1,0,1],[0,1,0],[1,0,1]]\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= rows\u00a0&lt;= 150<\/code><\/li><li><code>1 &lt;= columns\u00a0&lt;= 150<\/code><\/li><li><code>0 &lt;= mat[i][j] &lt;= 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force w\/ Pruning<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m^2*n^2)<br>Space complexity: O(1)<\/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 numSubmat(vector<vector<int>>& mat) {    \n    const int R = mat.size();\n    const int C = mat[0].size();\n    \/\/ # of sub matrices with top-left at (sr, sc)\n    auto subMats = [&](int sr, int sc) {\n      int max_c = C;\n      int count = 0;\n      for (int r = sr; r < R; ++r)\n        for (int c = sc; c < max_c; ++c)\n          if (mat[r][c]) {\n            ++count;\n          } else {\n            max_c = c;\n          }\n      return count;\n    };    \n    int ans = 0;\n    for (int r = 0; r < R; ++r)\n      for (int c = 0; c < C; ++c)\n        ans += subMats(r, c);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;rows * columns&nbsp;matrix&nbsp;mat&nbsp;of ones and zeros, return how many&nbsp;submatrices&nbsp;have all ones. Example 1: Input: mat = [[1,0,1], &nbsp; [1,1,0], &nbsp; [1,1,0]] Output: 13 Explanation:&#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,216,177,180],"class_list":["post-7031","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-matrix","tag-medium","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7031","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=7031"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7031\/revisions"}],"predecessor-version":[{"id":7034,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7031\/revisions\/7034"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}