{"id":2370,"date":"2018-03-24T19:51:41","date_gmt":"2018-03-25T02:51:41","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2370"},"modified":"2018-03-24T19:54:56","modified_gmt":"2018-03-25T02:54:56","slug":"leetcode-807-max-increase-to-keep-city-skyline","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-807-max-increase-to-keep-city-skyline\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 807. Max Increase to Keep City Skyline"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u5730\u56fe\u548c\u6bcf\u4e2a\u5750\u6807\u7684\u5927\u697c\u9ad8\u5ea6\uff0c\u6c42\u51fa\u603b\u589e\u9ad8\u7684\u6700\u5927\u503c\u4f7f\u5f97\u57ce\u5e02\u7684\u5929\u9645\u7ebf\uff08\u6295\u5f71\uff09\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p>In a 2 dimensional array\u00a0<code>grid<\/code>, each value\u00a0<code>grid[i][j]<\/code>\u00a0represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts\u00a0can be different for different buildings). Height\u00a00 is considered to be a\u00a0building\u00a0as well.<\/p>\n<p>At the end, the &#8220;skyline&#8221; when viewed from all four directions\u00a0of the grid, i.e.\u00a0top, bottom, left, and right,\u00a0must be the same as the\u00a0skyline of the original grid. A city&#8217;s skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See\u00a0the following example.<\/p>\n<p>What is the maximum total sum that the height of the buildings can be increased?<\/p>\n<pre class=\"crayon:false \"><strong>Example:<\/strong>\r\n<strong>Input:<\/strong> grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]\r\n<strong>Output:<\/strong> 35\r\n<strong>Explanation:<\/strong> \r\nThe grid is:\r\n[ [3, 0, 8, 4], \r\n  [2, 4, 5, 7],\r\n  [9, 2, 6, 3],\r\n  [0, 3, 1, 0] ]\r\n\r\nThe skyline viewed from top or bottom is: [9, 4, 8, 7]\r\nThe skyline viewed from left or right is: [8, 7, 9, 3]\r\n\r\nThe grid after increasing the height of buildings without affecting skylines is:\r\n\r\ngridNew = [ [8, 4, 8, 7],\r\n            [7, 4, 7, 7],\r\n            [9, 4, 8, 7],\r\n            [3, 3, 3, 3] ]\r\n\r\n<\/pre>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li><code>1 &lt; grid.length = grid[0].length &lt;= 50<\/code>.<\/li>\n<li>All heights\u00a0<code>grid[i][j]<\/code>\u00a0are in the range\u00a0<code>[0, 100]<\/code>.<\/li>\n<li>All buildings in\u00a0<code>grid[i][j]<\/code>\u00a0occupy the entire grid cell: that is, they are a\u00a0<code>1 x 1 x grid[i][j]<\/code>\u00a0rectangular prism.<\/li>\n<\/ul>\n<h1><strong>Solution: Greedy<\/strong><\/h1>\n<p>Find the max of each row and column, increase the height of building at i,j to min(max_row[i], max_col[j]).<\/p>\n<p>Time Complexity: O(m*n)<\/p>\n<p>Space complexity: O(m+n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int maxIncreaseKeepingSkyline(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {\r\n    int m = grid.size();\r\n    int n = grid[0].size();\r\n    vector&lt;int&gt; x(n, 0);\r\n    vector&lt;int&gt; y(n, 0);\r\n    for (int i = 0; i &lt; m; ++i)\r\n      for (int j = 0; j &lt; n; ++j) {\r\n        x[j] = max(x[j], grid[i][j]);\r\n        y[i] = max(y[i], grid[i][j]);\r\n      }\r\n    int ans = 0;\r\n    for (int i = 0; i &lt; m; ++i)\r\n      for (int j = 0; j &lt; n; ++j) \r\n        ans += min(x[i], y[j]) - grid[i][j];\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u5730\u56fe\u548c\u6bcf\u4e2a\u5750\u6807\u7684\u5927\u697c\u9ad8\u5ea6\uff0c\u6c42\u51fa\u603b\u589e\u9ad8\u7684\u6700\u5927\u503c\u4f7f\u5f97\u57ce\u5e02\u7684\u5929\u9645\u7ebf\uff08\u6295\u5f71\uff09\u4fdd\u6301\u4e0d\u53d8\u3002 In a 2 dimensional array\u00a0grid, each value\u00a0grid[i][j]\u00a0represents the height of a building located there. We are allowed to increase the height of any&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,278,177,103],"class_list":["post-2370","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-grid","tag-medium","tag-skyline","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2370","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=2370"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"predecessor-version":[{"id":2376,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2370\/revisions\/2376"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}