{"id":3432,"date":"2018-08-04T20:06:30","date_gmt":"2018-08-05T03:06:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3432"},"modified":"2018-08-04T20:06:48","modified_gmt":"2018-08-05T03:06:48","slug":"leetcode-887-projection-area-of-3d-shapes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-887-projection-area-of-3d-shapes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 887. Projection Area of 3D Shapes"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>On a\u00a0<code>N\u00a0*\u00a0N<\/code>\u00a0grid, we place some\u00a0<code>1 * 1 * 1\u00a0<\/code>cubes that are axis-aligned with the x, y, and z axes.<\/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>Now we view the\u00a0<em>projection<\/em>\u00a0of these cubes\u00a0onto the xy, yz, and zx planes.<\/p>\n<p>A projection is like a shadow, that\u00a0maps our 3 dimensional figure to a 2 dimensional plane.<\/p>\n<p>Here, we are viewing the &#8220;shadow&#8221; when looking at the cubes from the top, the front, and the side.<\/p>\n<p>Return the total area of all three projections.<\/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\">5<\/span>\r\n<\/pre>\n<div>\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\">17<\/span>\r\n<strong>Explanation: <\/strong>\r\nHere are the three projections (\"shadows\") of the shape made with each axis-aligned plane.\r\n<img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/08\/02\/shadow.png\" alt=\"\" \/>\r\n<\/pre>\n<div>\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\">8<\/span>\r\n<\/pre>\n<div>\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\">14<\/span>\r\n<\/pre>\n<div>\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\">21<\/span>\r\n<\/pre>\n<div>\n<div>\n<div>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= grid.length = grid[0].length\u00a0&lt;= 50<\/code><\/li>\n<li><code>0 &lt;= grid[i][j] &lt;= 50<\/code><\/li>\n<\/ul>\n<h1><strong>Solution: Brute Force<\/strong><\/h1>\n<p>Sum of max heights for each cols \/ rows + # of non-zero-height bars.<\/p>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time : 4 ms\r\nclass Solution {\r\npublic:\r\n  int projectionArea(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {\r\n    int n = grid.size();    \r\n    int m = grid[0].size();\r\n    int ans = 0;\r\n    for (int i = 0; i &lt; n; ++i) {\r\n      int h = 0;\r\n      for (int j = 0; j &lt; m; ++j) {\r\n        h = max(h, grid[i][j]);\r\n        if (grid[i][j] != 0) ++ans;\r\n      }\r\n      ans += h;      \r\n    }\r\n    for (int j = 0; j &lt; m; ++j) {\r\n      int h = 0;\r\n      for (int i = 0; i &lt; n; ++i)\r\n        h = max(h, grid[i][j]);\r\n      ans += h;      \r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem On a\u00a0N\u00a0*\u00a0N\u00a0grid, we place some\u00a01 * 1 * 1\u00a0cubes that are axis-aligned with the x, y, and z axes. Each value\u00a0v = grid[i][j]\u00a0represents a&#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":[222,284,363,362,364],"class_list":["post-3432","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-easy","tag-geometry","tag-historgram","tag-projection","tag-shadow","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3432","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=3432"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3432\/revisions"}],"predecessor-version":[{"id":3434,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3432\/revisions\/3434"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}