{"id":5970,"date":"2019-12-17T20:12:31","date_gmt":"2019-12-18T04:12:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5970"},"modified":"2019-12-17T20:25:11","modified_gmt":"2019-12-18T04:25:11","slug":"leetcode-1292-maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1292-maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold"},"content":{"rendered":"\n<p>Given a&nbsp;<code>m x n<\/code>&nbsp;matrix&nbsp;<code>mat<\/code>&nbsp;and an integer&nbsp;<code>threshold<\/code>. Return the maximum side-length of a square with a sum less than or equal to&nbsp;<code>threshold<\/code>&nbsp;or return&nbsp;<strong>0<\/strong>&nbsp;if there is no such square.<\/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\/2019\/12\/05\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The maximum side length of square with sum less than 4 is 2 as shown.\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 = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1\n<strong>Output:<\/strong> 0\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,0,0,0],[1,0,0,0],[1,0,0,0]], threshold = 6\n<strong>Output:<\/strong> 3\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 = [[18,70],[61,1],[25,85],[14,40],[11,96],[97,96],[63,45]], threshold = 40184\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= m, n &lt;= 300<\/code><\/li><li><code>m == mat.length<\/code><\/li><li><code>n == mat[i].length<\/code><\/li><li><code>0 &lt;= mat[i][j] &lt;= 10000<\/code><\/li><li><code>0 &lt;= threshold&nbsp;&lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP + Brute Force<\/strong><\/h2>\n\n\n\n<p>Precompute the sums of sub-matrixes whose left-top corner is at (0,0).<\/p>\n\n\n\n<p>Try all possible left-top corner and sizes.<\/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, 148 ms\nclass Solution {\npublic:\n  int maxSideLength(vector<vector<int>>& mat, int threshold) {\n    const int m = mat.size();\n    const int n = mat[0].size();\n    \n    vector<vector<int>> dp(m + 1, vector<int>(n + 1));\n    for (int y = 1; y <= m; ++y)\n      for (int x = 1; x <= n; ++x)\n        dp[y][x] = dp[y][x - 1] + dp[y - 1][x]  - dp[y - 1][x - 1] + mat[y - 1][x - 1];\n    \n    auto rangeSum = [&#038;](int x1, int y1, int x2, int y2) {\n      return dp[y2][x2] - dp[y2][x1 - 1] - dp[y1 - 1][x2] + dp[y1 - 1][x1 - 1];\n    };\n    \n    int ans = 0;\n    for (int y = 1; y <= m; ++y)\n      for (int x = 1; x <= n; ++x)\n        for (int k = 0; y + k <= m &#038;&#038; x + k <= n; ++k) {\n          if (rangeSum(x, y, x + k, y + k) > threshold) break;\n          ans = max(ans, k + 1);\n        }\n    return ans;  \n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Binary Search<\/strong><\/h2>\n\n\n\n<p>Search for the smallest size k that is greater than the threshold, ans = k &#8211; 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, 116 ms\nclass Solution {\npublic:\n  int maxSideLength(vector<vector<int>>& mat, int threshold) {\n    const int m = mat.size();\n    const int n = mat[0].size();\n    \n    vector<vector<int>> dp(m + 1, vector<int>(n + 1));\n    for (int y = 1; y <= m; ++y)\n      for (int x = 1; x <= n; ++x)\n        dp[y][x] = dp[y][x - 1] + dp[y - 1][x]  - dp[y - 1][x - 1] + mat[y - 1][x - 1];\n    \n    auto rangeSum = [&#038;](int x1, int y1, int x2, int y2) {\n      return dp[y2][x2] - dp[y2][x1 - 1] - dp[y1 - 1][x2] + dp[y1 - 1][x1 - 1];\n    };\n    \n    int ans = 0;\n    for (int y = 1; y <= m; ++y)\n      for (int x = 1; x <= n; ++x) {\n        int l = 0;\n        int r = min(m - y, n - x) + 1;\n        while (l < r) {\n          int m = l + (r - l) \/ 2;\n          \/\/ Find smllest l that > threshold, ans = (l + 1) - 1\n          if (rangeSum(x, y, x + m, y + m) > threshold)\n            r = m;\n          else\n            l = m + 1;\n        }\n        ans = max(ans, (l + 1) - 1);\n      }\n    return ans;  \n  }\n};\n\n\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 3: Bounded Search<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m*n + min(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, 148 ms\nclass Solution {\npublic:\n  int maxSideLength(vector<vector<int>>& mat, int threshold) {\n    const int m = mat.size();\n    const int n = mat[0].size();\n    \n    vector<vector<int>> dp(m + 1, vector<int>(n + 1));\n    for (int y = 1; y <= m; ++y)\n      for (int x = 1; x <= n; ++x)\n        dp[y][x] = dp[y][x - 1] + dp[y - 1][x]  - dp[y - 1][x - 1] + mat[y - 1][x - 1];\n    \n    auto rangeSum = [&#038;](int x1, int y1, int x2, int y2) {\n      return dp[y2][x2] - dp[y2][x1 - 1] - dp[y1 - 1][x2] + dp[y1 - 1][x1 - 1];\n    };\n    \n    int ans = 0;\n    for (int y = 1; y <= m; ++y)\n      for (int x = 1; x <= n; ++x)\n        for (int k = ans; y + k <= m &#038;&#038; x + k <= n; ++k) {\n          if (rangeSum(x, y, x + k, y + k) > threshold) break;\n          ans = max(ans, k + 1);\n        }\n    return ans;  \n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;m x n&nbsp;matrix&nbsp;mat&nbsp;and an integer&nbsp;threshold. Return the maximum side-length of a square with a sum less than or equal to&nbsp;threshold&nbsp;or return&nbsp;0&nbsp;if there is no&#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":[52,18,216,177,98],"class_list":["post-5970","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-binary-search","tag-dp","tag-matrix","tag-medium","tag-range-query","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5970","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=5970"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5970\/revisions"}],"predecessor-version":[{"id":5973,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5970\/revisions\/5973"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}