<?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>area Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/area/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/area/</link>
	<description></description>
	<lastBuildDate>Wed, 19 Sep 2018 07:25:35 +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>area Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/area/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 223. Rectangle Area</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-223-rectangle-area/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-223-rectangle-area/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 19 Sep 2018 07:24:55 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[area]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rectangle]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4032</guid>

					<description><![CDATA[<p>Problem Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-223-rectangle-area/">花花酱 LeetCode 223. Rectangle Area</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>Find the total area covered by two <strong>rectilinear</strong> rectangles in a <strong>2D</strong> plane.</p>
<p>Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.</p>
<p><img src="https://leetcode.com/static/images/problemset/rectangle_area.png" alt="Rectangle Area" /></p>
<p><strong>Example:</strong></p>
<pre class="crayon:false "><strong>Input: </strong>A = <span id="example-input-1-1">-3</span>, B = <span id="example-input-1-2">0</span>, C = <span id="example-input-1-3">3</span>, D = <span id="example-input-1-4">4</span>, E = <span id="example-input-1-5">0</span>, F = <span id="example-input-1-6">-1</span>, G = <span id="example-input-1-7">9</span>, H = <span id="example-input-1-8">2</span>
<strong>Output: </strong><span id="example-output-1">45</span></pre>
<p><strong>Note:</strong></p>
<p>Assume that the total area is never beyond the maximum possible value of <strong>int</strong>.</p>
<h1>Solution:</h1>
<p>area1 + area2 &#8211; overlapped area</p>
<p>Time complexity: O(1)</p>
<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
class Solution {
public:
  int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    int area1 = (C - A) * (D - B);
    int area2 = (G - E) * (H - F);
    int x1 = max(A, E);
    int x2 = max(x1, min(C, G));
    int y1 = max(B, F);
    int y2 = max(y1, min(D, H));
    return area1 + area2 - (x2 - x1) * (y2 - y1);
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    int area1 = (C - A) * (D - B);
    int area2 = (G - E) * (H - F);
    int x1 = Math.max(A, E);
    int x2 = Math.max(x1, Math.min(C, G));
    int y1 = Math.max(B, F);
    int y2 = Math.max(y1, Math.min(D, H));
    return area1 + area2 - (x2 - x1) * (y2 - y1);
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def computeArea(self, A, B, C, D, E, F, G, H):
    area1 = (C - A) * (D - B);
    area2 = (G - E) * (H - F);
    x1 = max(A, E);
    x2 = max(x1, min(C, G));
    y1 = max(B, F);
    y2 = max(y1, min(D, H));
    return area1 + area2 - (x2 - x1) * (y2 - y1)</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-223-rectangle-area/">花花酱 LeetCode 223. Rectangle Area</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-223-rectangle-area/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
		<item>
		<title>花花酱 LeetCode 812. Largest Triangle Area</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-812-largest-triangle-area/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-812-largest-triangle-area/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 08 Apr 2018 07:10:57 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[area]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[triangle]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2446</guid>

					<description><![CDATA[<p>Problem 题目大意：给你一些点，求用这些点能够成的最大的三角形面积是多少？ https://leetcode.com/problems/largest-triangle-area/description/ You have a list of points in the plane. Return the area of the largest triangle that can be formed by any&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-812-largest-triangle-area/">花花酱 LeetCode 812. Largest Triangle Area</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>题目大意：给你一些点，求用这些点能够成的最大的三角形面积是多少？</p>
<p><a href="https://leetcode.com/problems/largest-triangle-area/description/">https://leetcode.com/problems/largest-triangle-area/description/</a></p>
<p>You have a list of points in the plane. Return the area of the largest triangle that can be formed by any 3 of the points.</p>
<pre class="crayon:false"><strong>Example:</strong>
<strong>Input:</strong> points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
The five points are show in the figure below. The red triangle is the largest.
</pre>
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/04/1027.png" alt="" /></p>
<p><strong>Notes:</strong></p>
<ul>
<li><code>3 &lt;= points.length &lt;= 50</code>.</li>
<li>No points will be duplicated.</li>
<li> <code>-50 &lt;= points[i][j] &lt;= 50</code>.</li>
<li>Answers within <code>10^-6</code> of the true value will be accepted as correct.</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: Brute Force</strong></h1>
<p>Time complexity: O(n^3)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 11 ms
class Solution {
public:
  double largestTriangleArea(vector&lt;vector&lt;int&gt;&gt;&amp; points) {
    const int n = points.size();
    double best = 0.0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        for (int k = j + 1; k &lt; n; ++k) {          
          const double a = dist(points[i], points[j]);
          const double b = dist(points[i], points[k]);
          const double c = dist(points[j], points[k]);
          const double s = (a + b + c) / 2;
          const double area = sqrt(s * (s - a) * (s - b) * (s - c));          
          best = max(best, area);
        }
    return best;
  }
private:
  static inline double dist(const vector&lt;int&gt;&amp; p1, const vector&lt;int&gt;&amp; p2) {
    return sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]));
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-812-largest-triangle-area/">花花酱 LeetCode 812. Largest Triangle Area</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-812-largest-triangle-area/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 11. Container With Most Water</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-11-container-with-most-water/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-11-container-with-most-water/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Dec 2017 09:00:38 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[area]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1077</guid>

					<description><![CDATA[<p>Problem: Given n non-negative integers a1, a2, &#8230;, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-11-container-with-most-water/">花花酱 LeetCode 11. Container With Most Water</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/IONgE6QZgGI?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Given <i>n</i> non-negative integers <i>a<sub>1</sub></i>, <i>a<sub>2</sub></i>, &#8230;, <i>a<sub>n</sub></i>, where each represents a point at coordinate (<i>i</i>, <i>a<sub>i</sub></i>). <i>n</i> vertical lines are drawn such that the two endpoints of line <i>i</i> is at (<i>i</i>, <i>a<sub>i</sub></i>) and (<i>i</i>, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.</p>
<p>Note: You may not slant the container and <i>n</i> is at least 2.</p>
<p><strong>Examples:</strong></p>
<p>input: [1 3 2 4]<br />
output: 6<br />
explanation: use 3, 4, we have the following, which contains (4th-2nd) * min(3, 4) = 2 * 3 = 6 unit of water.</p><pre class="crayon-plain-tag">|
|~~|
|~~|&nbsp;
|~~|</pre><p><strong>Idea:</strong></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/11-ep121.png"><img class="alignnone size-full wp-image-1087" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/11-ep121.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/11-ep121.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/11-ep121-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/11-ep121-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p>Two pointers</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><strong>Solution:</strong></p>
<p>C++ two pointers</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 19 ms
class Solution {
public:
    int maxArea(const vector&lt;int&gt;&amp; height) {
        int ans = 0;
        int l = 0;
        int r = height.size() - 1;
        while (l &lt; r) {
            int h = min(height[l], height[r]);
            ans = max(ans, h * (r - l));
            if (height[l] &lt; height[r]) 
                ++l;
            else
                --r;
        }
        return ans;
    }
};</pre><p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 13 ms
class Solution {
    public int maxArea(int[] height) {
        int l = 0;
        int r = height.length - 1;
        int ans = 0;
        while (l &lt; r) {
            int h = Math.min(height[l], height[r]);
            ans = Math.max(ans, h * (r - l));
            if (height[l] &lt; height[r])
                ++l;
            else
                --r;
        }
        return ans;
    }
}</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-11-container-with-most-water/">花花酱 LeetCode 11. Container With Most Water</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/two-pointers/leetcode-11-container-with-most-water/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
