{"id":3701,"date":"2018-08-26T08:25:33","date_gmt":"2018-08-26T15:25:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3701"},"modified":"2018-08-30T09:32:26","modified_gmt":"2018-08-30T16:32:26","slug":"leetcode-892-surface-area-of-3d-shapes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-892-surface-area-of-3d-shapes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 892. Surface Area of 3D Shapes"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>On a\u00a0<code>N\u00a0*\u00a0N<\/code>\u00a0grid, we place some\u00a0<code>1 * 1 * 1\u00a0<\/code>cubes.<\/p>\n<p>Each value\u00a0<code>v = grid[i][j]<\/code>\u00a0represents a tower of\u00a0<code>v<\/code>\u00a0cubes placed on top of grid cell\u00a0<code>(i, j)<\/code>.<\/p>\n<p>Return the total surface area of the resulting shapes.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[[2]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">10<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[[1,2],[3,4]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">34<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">[[1,0],[0,2]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">16<\/span>\r\n<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-4-1\">[[1,1,1],[1,0,1],[1,1,1]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-4\">32<\/span>\r\n<\/pre>\n<p><strong>Example 5:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-5-1\">[[2,2,2],[2,1,2],[2,2,2]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-5\">46<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= N &lt;= 50<\/code><\/li>\n<li><code>0 &lt;= grid[i][j] &lt;= 50<\/code><\/li>\n<\/ul>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Solution: Geometry<\/strong><\/h1>\n<p>3D version of\u00a0<a href=\"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-463-island-perimeter\/\">\u82b1\u82b1\u9171 LeetCode 463. Island Perimeter<\/a><\/p>\n<p>Ans = total faces &#8211; hidden faces.<\/p>\n<p>each pile with height h has the surface area of 2 (top\/bottom) + 4*h (sides)<\/p>\n\\(ans = ans + 2 + 4 * h\\)\n<p>if a cube has a neighbour, reduce the total surface area by 1<\/p>\n<p>For each pile, we check 4 neighbours, the number of total hidden faces of this pile is sum(min(h, neighbour&#8217;s h)).<\/p>\n\\(ans = ans &#8211; \\Sigma min(h, n.h)\\)\n<p>Time complexity: O(mn)<\/p>\n<div>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int surfaceArea(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {\r\n    static const vector&lt;int&gt; dirs{0, -1, 0, 1, 0};\r\n    int m = grid.size();\r\n    int n = grid[0].size();\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        int h = grid[i][j];\r\n        if (h == 0) continue;\r\n        ans += 2 + 4 * h;        \r\n        for (int k = 0; k &lt; 4; k++) {\r\n          int tx = j + dirs[k];\r\n          int ty = i + dirs[k + 1];\r\n          if (tx &lt; 0 || tx &gt;= n || ty &lt; 0 || ty &gt;= m) continue;\r\n          int th = grid[ty][tx];          \r\n          ans -= (th &lt;= 0 ? 0 : min(h, th));\r\n        }\r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ (opt)<\/h2>\n<div class=\"tabcontent\">\n\n<p>Since the neighbor relationship is symmetric, we can only consider the top and left neighbors and double the hidden faces.<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int surfaceArea(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {    \r\n    int m = grid.size();\r\n    int n = grid[0].size();\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        int h = grid[i][j];\r\n        if (h == 0) continue;\r\n        ans += 2 + 4 * h;\r\n        if (i) ans -= 2 * min(h, grid[i - 1][j]);\r\n        if (j) ans -= 2 * min(h, grid[i][j - 1]);        \r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem On a\u00a0N\u00a0*\u00a0N\u00a0grid, we place some\u00a01 * 1 * 1\u00a0cubes. Each value\u00a0v = grid[i][j]\u00a0represents a tower of\u00a0v\u00a0cubes placed on top of grid cell\u00a0(i, j). Return&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[178,222,284,386],"class_list":["post-3701","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-area","tag-easy","tag-geometry","tag-surface","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3701","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=3701"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3701\/revisions"}],"predecessor-version":[{"id":3756,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3701\/revisions\/3756"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}