<?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>triangle Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/triangle/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/triangle/</link>
	<description></description>
	<lastBuildDate>Sun, 24 Jan 2021 06:57:48 +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>triangle Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/triangle/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1739. Building Boxes</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1739-building-boxes/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1739-building-boxes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Jan 2021 06:48:28 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[pyramid]]></category>
		<category><![CDATA[triangle]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8034</guid>

					<description><![CDATA[<p>You have a cubic storeroom where the width, length, and height of the room are all equal to&#160;n&#160;units. You are asked to place&#160;n&#160;boxes in this&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1739-building-boxes/">花花酱 LeetCode 1739. Building Boxes</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>You have a cubic storeroom where the width, length, and height of the room are all equal to&nbsp;<code>n</code>&nbsp;units. You are asked to place&nbsp;<code>n</code>&nbsp;boxes in this room where each box is a cube of unit side length. There are however some rules to placing the boxes:</p>



<ul><li>You can place the boxes anywhere on the floor.</li><li>If box&nbsp;<code>x</code>&nbsp;is placed on top of the box&nbsp;<code>y</code>, then each side of the four vertical sides of the box&nbsp;<code>y</code>&nbsp;<strong>must</strong>&nbsp;either be adjacent to another box or to a wall.</li></ul>



<p>Given an integer&nbsp;<code>n</code>, return<em>&nbsp;the&nbsp;<strong>minimum</strong>&nbsp;possible number of boxes touching the floor.</em></p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/01/04/3-boxes.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> The figure above is for the placement of the three boxes.
These boxes are placed in the corner of the room, where the corner is on the left side.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/01/04/4-boxes.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> The figure above is for the placement of the four boxes.
These boxes are placed in the corner of the room, where the corner is on the left side.
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/01/04/10-boxes.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 10
<strong>Output:</strong> 6
<strong>Explanation:</strong> The figure above is for the placement of the ten boxes.
These boxes are placed in the corner of the room, where the corner is on the back side.</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li></ul>



<p></p>



<h2><strong>Solution: Geometry</strong></h2>



<p>Step 1: Build a largest pyramid that has less then n cubes, whose base area is d*(d+1) / 2<br>Step 2: Build a largest triangle with cubes left, whose base area is l, l*(l + 1) / 2 &gt;= left</p>



<p>Time complexity: O(n^(1/3))<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minimumBoxes(int n) {
    int d = 0;
    int l = 0;
    while (n - (d + 1) * (d + 2) / 2 &gt; 0) {
      n -= (d + 1) * (d + 2) / 2;
      ++d;
    }
    while (n &gt; 0) n -= ++l;
    return d * (d + 1) / 2 + l;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1739-building-boxes/">花花酱 LeetCode 1739. Building Boxes</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-1739-building-boxes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 976. Largest Perimeter Triangle</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-976-largest-perimeter-triangle/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-976-largest-perimeter-triangle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 15 Jan 2019 04:50:26 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[triangle]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4640</guid>

					<description><![CDATA[<p>Given an array&#160;A&#160;of positive lengths, return the largest perimeter of a triangle with&#160;non-zero area, formed from 3 of these lengths. If it is impossible to&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-976-largest-perimeter-triangle/">花花酱 LeetCode 976. Largest Perimeter Triangle</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>Given an array&nbsp;<code>A</code>&nbsp;of positive lengths, return the largest perimeter of a triangle with&nbsp;<strong>non-zero area</strong>, formed from 3 of these lengths.</p>



<p>If it is impossible to form any&nbsp;triangle of non-zero area, return&nbsp;<code>0</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[2,1,2]
<strong>Output: </strong>5
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[1,2,1]
<strong>Output: </strong>0
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[3,2,3,4]
<strong>Output: </strong>10
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[3,6,2,3]
<strong>Output: </strong>8
</pre>



<p><strong>Note:</strong></p>



<ol><li><code>3 &lt;= A.length &lt;= 10000</code></li><li><code>1 &lt;= A[i] &lt;= 10^6</code></li></ol>



<h2><strong>Solution:&nbsp;Greedy</strong></h2>



<p>Answer must be 3 consecutive numbers in the sorted array<br>if A[i] &gt;= A[i+1] + A[i+2], then A[i] &gt;= A[i+j] + A[i+k],  1 &lt; j &lt; k<br>if A[i] &lt; A[i+1] + A[i+2], then A[i] + A[i+1] + A[i+2] is the answer</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int largestPerimeter(vector&lt;int&gt;&amp; A) {
    sort(rbegin(A), rend(A));
    for (int i = 0; i &lt; A.size() - 2; ++i)
      if (A[i] &lt; A[i + 1] + A[i + 2])
        return A[i] + A[i + 1] + A[i + 2];
    return 0;
  }
};</pre>
</div></div>



<p> </p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-976-largest-perimeter-triangle/">花花酱 LeetCode 976. Largest Perimeter Triangle</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/greedy/leetcode-976-largest-perimeter-triangle/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>
	</channel>
</rss>
