<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>surface Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/surface/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/surface/</link>
	<description></description>
	<lastBuildDate>Thu, 30 Aug 2018 16:32:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>surface Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/surface/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 892. Surface Area of 3D Shapes</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-892-surface-area-of-3d-shapes/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-892-surface-area-of-3d-shapes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Aug 2018 15:25:33 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[area]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[surface]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3701</guid>

					<description><![CDATA[<p>Problem On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j). Return&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-892-surface-area-of-3d-shapes/">花花酱 LeetCode 892. Surface Area of 3D Shapes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>On a <code>N * N</code> grid, we place some <code>1 * 1 * 1 </code>cubes.</p>
<p>Each value <code>v = grid[i][j]</code> represents a tower of <code>v</code> cubes placed on top of grid cell <code>(i, j)</code>.</p>
<p>Return the total surface area of the resulting shapes.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[[2]]</span>
<strong>Output: </strong><span id="example-output-1">10</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[[1,2],[3,4]]</span>
<strong>Output: </strong><span id="example-output-2">34</span>
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">[[1,0],[0,2]]</span>
<strong>Output: </strong><span id="example-output-3">16</span>
</pre>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-4-1">[[1,1,1],[1,0,1],[1,1,1]]</span>
<strong>Output: </strong><span id="example-output-4">32</span>
</pre>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-5-1">[[2,2,2],[2,1,2],[2,2,2]]</span>
<strong>Output: </strong><span id="example-output-5">46</span>
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>1 &lt;= N &lt;= 50</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 50</code></li>
</ul>
<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"> </ins></p>
<h1><strong>Solution: Geometry</strong></h1>
<p>3D version of <a href="https://zxi.mytechroad.com/blog/math/leetcode-463-island-perimeter/">花花酱 LeetCode 463. Island Perimeter</a></p>
<p>Ans = total faces &#8211; hidden faces.</p>
<p>each pile with height h has the surface area of 2 (top/bottom) + 4*h (sides)</p>
\(ans = ans + 2 + 4 * h\)
<p>if a cube has a neighbour, reduce the total surface area by 1</p>
<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>
\(ans = ans &#8211; \Sigma min(h, n.h)\)
<p>Time complexity: O(mn)</p>
<div>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int surfaceArea(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    static const vector&lt;int&gt; dirs{0, -1, 0, 1, 0};
    int m = grid.size();
    int n = grid[0].size();
    int ans = 0;
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j) {
        int h = grid[i][j];
        if (h == 0) continue;
        ans += 2 + 4 * h;        
        for (int k = 0; k &lt; 4; k++) {
          int tx = j + dirs[k];
          int ty = i + dirs[k + 1];
          if (tx &lt; 0 || tx &gt;= n || ty &lt; 0 || ty &gt;= m) continue;
          int th = grid[ty][tx];          
          ans -= (th &lt;= 0 ? 0 : min(h, th));
        }
      }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">C++ (opt)</h2>
<div class="tabcontent">
</p>
<p>Since the neighbor relationship is symmetric, we can only consider the top and left neighbors and double the hidden faces.</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int surfaceArea(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {    
    int m = grid.size();
    int n = grid[0].size();
    int ans = 0;
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j) {
        int h = grid[i][j];
        if (h == 0) continue;
        ans += 2 + 4 * h;
        if (i) ans -= 2 * min(h, grid[i - 1][j]);
        if (j) ans -= 2 * min(h, grid[i][j - 1]);        
      }
    return ans;
  }
};</pre><p></div></div></p>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-892-surface-area-of-3d-shapes/">花花酱 LeetCode 892. Surface Area of 3D Shapes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/geometry/leetcode-892-surface-area-of-3d-shapes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
