{"id":6071,"date":"2020-01-11T10:28:35","date_gmt":"2020-01-11T18:28:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6071"},"modified":"2020-01-11T10:29:08","modified_gmt":"2020-01-11T18:29:08","slug":"leetcode-1314-matrix-block-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1314-matrix-block-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1314. Matrix Block Sum"},"content":{"rendered":"\n<p>Given a&nbsp;<code>m * n<\/code>&nbsp;matrix&nbsp;<code>mat<\/code>&nbsp;and an integer&nbsp;<code>K<\/code>, return a matrix&nbsp;<code>answer<\/code>&nbsp;where each&nbsp;<code>answer[i][j]<\/code>&nbsp;is the sum of all elements&nbsp;<code>mat[r][c]<\/code>&nbsp;for&nbsp;<code>i - K &lt;= r &lt;= i + K, j - K &lt;= c &lt;= j + K<\/code>, and&nbsp;<code>(r, c)<\/code>&nbsp;is a valid position in the matrix.<\/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,2,3],[4,5,6],[7,8,9]], K = 1\n<strong>Output:<\/strong> [[12,21,16],[27,45,33],[24,39,28]]\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 = [[1,2,3],[4,5,6],[7,8,9]], K = 2\n<strong>Output:<\/strong> [[45,45,45],[45,45,45],[45,45,45]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m ==&nbsp;mat.length<\/code><\/li><li><code>n ==&nbsp;mat[i].length<\/code><\/li><li><code>1 &lt;= m, n, K &lt;= 100<\/code><\/li><li><code>1 &lt;= mat[i][j] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: 2D range query<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(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, 20 ms\nclass Solution {\npublic:\n  vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int K) {\n    const int n = mat.size();\n    const int m = mat[0].size();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1));\n    for (int y = 1; y <= n; ++y)\n      for (int x = 1; x <= m; ++x)\n        dp[y][x] = dp[y - 1][x] + dp[y][x - 1] + mat[y - 1][x - 1] - dp[y - 1][x - 1];\n    auto ans = mat;\n    for (int y = 1; y <= n; ++y)\n      for (int x = 1; x <= m; ++x) {\n        int x1 = max(1, x - K);\n        int x2 = min(m, x + K);\n        int y1 = max(1, y - K);\n        int y2 = min(n, y + K);\n        ans[y - 1][x - 1] = dp[y2][x2] - dp[y1 - 1][x2] - dp[y2][x1 - 1] + dp[y1 - 1][x1 - 1];\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;m * n&nbsp;matrix&nbsp;mat&nbsp;and an integer&nbsp;K, return a matrix&nbsp;answer&nbsp;where each&nbsp;answer[i][j]&nbsp;is the sum of all elements&nbsp;mat[r][c]&nbsp;for&nbsp;i &#8211; K &lt;= r &lt;= i + K, j -&#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,366,140,201],"class_list":["post-6071","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-matrix","tag-medium","tag-omn","tag-query","tag-range-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6071","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=6071"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6071\/revisions"}],"predecessor-version":[{"id":6073,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6071\/revisions\/6073"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}