{"id":7990,"date":"2021-01-17T13:10:47","date_gmt":"2021-01-17T21:10:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7990"},"modified":"2021-01-17T16:55:18","modified_gmt":"2021-01-18T00:55:18","slug":"leetcode-1727-largest-submatrix-with-rearrangements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1727-largest-submatrix-with-rearrangements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1727. Largest Submatrix With Rearrangements"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1727. Largest Submatrix With Rearrangements - \u5237\u9898\u627e\u5de5\u4f5c EP380\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/eX0lXwGu3OE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given a binary matrix&nbsp;<code>matrix<\/code>&nbsp;of size&nbsp;<code>m x n<\/code>, and you are allowed to rearrange the&nbsp;<strong>columns<\/strong>&nbsp;of the&nbsp;<code>matrix<\/code>&nbsp;in any order.<\/p>\n\n\n\n<p>Return&nbsp;<em>the area of the largest submatrix within&nbsp;<\/em><code>matrix<\/code><em>&nbsp;where&nbsp;<strong>every<\/strong>&nbsp;element of the submatrix is&nbsp;<\/em><code>1<\/code><em>&nbsp;after reordering the columns optimally.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/12\/29\/screenshot-2020-12-30-at-40536-pm.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> matrix = [[0,0,1],[1,1,1],[1,0,1]]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> You can rearrange the columns as shown above.\nThe largest submatrix of 1s, in bold, has an area of 4.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/12\/29\/screenshot-2020-12-30-at-40852-pm.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> matrix = [[1,0,1,0,1]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> You can rearrange the columns as shown above.\nThe largest submatrix of 1s, in bold, has an area of 3.\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> matrix = [[1,1,0],[1,0,1]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.<\/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> matrix = [[0,0],[0,0]]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> As there are no 1s, no submatrix of 1s can be formed and the area is 0.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == matrix.length<\/code><\/li><li><code>n == matrix[i].length<\/code><\/li><li><code>1 &lt;= m * n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>matrix[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 + Sorting<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-1.png\" alt=\"\" class=\"wp-image-7997\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-2.png\" alt=\"\" class=\"wp-image-7999\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1727-ep380-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Preprocess each column, for col j, matrix[i][j] := length consecutive ones of col j.<\/p>\n\n\n\n<pre class=\"wp-block-code;crayon:false\"><code>&#091;0,0,1]    &#091;0,0,1]\n&#091;1,1,1] =&gt; &#091;1,1,2]\n&#091;1,0,1]    &#091;2,0,3]<\/code><\/pre>\n\n\n\n<p>Then we enumerate ending row, for each ending row i, we sort row[i] in deceasing order<\/p>\n\n\n\n<p>e.g. i = 2<\/p>\n\n\n\n<pre class=\"wp-block-code;crayon:false\"><code>&#091;0,0,1]                  &#091;-,-,-]\n&#091;1,1,2] sort by row 2 =&gt; &#091;-,-,-]\n&#091;2,0,3]                  &#091;3,2,0]<\/code><\/pre>\n\n\n\n<p>row[2][1] = 3, means there is a 3&#215;1 all ones sub matrix, area = 3<br>row[2][2] = 2, means there is a 2&#215;2 all ones sub matrix, area = 4.<\/p>\n\n\n\n<p>Time complexity: O(m*n*log(n))<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 largestSubmatrix(vector<vector<int>>& matrix) {\n    const int m = matrix.size();\n    const int n = matrix[0].size();\n    for (int j = 0; j < n; ++j)\n      for (int i = 1; i < m; ++i)      \n        if (matrix[i][j]) matrix[i][j] += matrix[i - 1][j];          \n    \n    int ans = 0;\n    for (int i = 0; i < m; ++i) {\n      sort(rbegin(matrix[i]), rend(matrix[i]));\n      for (int j = 0; j < n; ++j)\n        ans = max(ans, (j + 1) * matrix[i][j]);        \n    }\n    return ans;    \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a binary matrix&nbsp;matrix&nbsp;of size&nbsp;m x n, and you are allowed to rearrange the&nbsp;columns&nbsp;of the&nbsp;matrix&nbsp;in any order. Return&nbsp;the area of the largest submatrix&#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,15],"class_list":["post-7990","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-matrix","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7990","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=7990"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7990\/revisions"}],"predecessor-version":[{"id":8000,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7990\/revisions\/8000"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}