<?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>gemoetry Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/gemoetry/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/gemoetry/</link>
	<description></description>
	<lastBuildDate>Sun, 17 Jan 2021 20:40:47 +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>gemoetry Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/gemoetry/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1725. Number Of Rectangles That Can Form The Largest Square</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1725-number-of-rectangles-that-can-form-the-largest-square/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1725-number-of-rectangles-that-can-form-the-largest-square/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 Jan 2021 20:39:55 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[gemoetry]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7983</guid>

					<description><![CDATA[<p>You are given an array&#160;rectangles&#160;where&#160;rectangles[i] = [li, wi]&#160;represents the&#160;ith&#160;rectangle of length&#160;li&#160;and width&#160;wi. You can cut the&#160;ith&#160;rectangle to form a square with a side length of&#160;k&#160;if&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1725-number-of-rectangles-that-can-form-the-largest-square/">花花酱 LeetCode 1725. Number Of Rectangles That Can Form The Largest Square</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 are given an array&nbsp;<code>rectangles</code>&nbsp;where&nbsp;<code>rectangles[i] = [l<sub>i</sub>, w<sub>i</sub>]</code>&nbsp;represents the&nbsp;<code>i<sup>th</sup></code>&nbsp;rectangle of length&nbsp;<code>l<sub>i</sub></code>&nbsp;and width&nbsp;<code>w<sub>i</sub></code>.</p>



<p>You can cut the&nbsp;<code>i<sup>th</sup></code>&nbsp;rectangle to form a square with a side length of&nbsp;<code>k</code>&nbsp;if both&nbsp;<code>k &lt;= l<sub>i</sub></code>&nbsp;and&nbsp;<code>k &lt;= w<sub>i</sub></code>. For example, if you have a rectangle&nbsp;<code>[4,6]</code>, you can cut it to get a square with a side length of at most&nbsp;<code>4</code>.</p>



<p>Let&nbsp;<code>maxLen</code>&nbsp;be the side length of the&nbsp;<strong>largest</strong>&nbsp;square you can obtain from any of the given rectangles.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>number</strong>&nbsp;of rectangles that can make a square with a side length of&nbsp;</em><code>maxLen</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> rectangles = [[5,8],[3,9],[5,12],[16,5]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The largest squares you can get from each rectangle are of lengths [5,3,5,5].
The largest possible square is of length 5, and you can get it out of 3 rectangles.
</pre>



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



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



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



<ul><li><code>1 &lt;= rectangles.length &lt;= 1000</code></li><li><code>rectangles[i].length == 2</code></li><li><code>1 &lt;= l<sub>i</sub>, w<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li><li><code>l<sub>i</sub>&nbsp;!= w<sub>i</sub></code></li></ul>



<h2><strong>Solution: Running Max of Shortest Edge</strong></h2>



<p>Time complexity: O(n)<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 countGoodRectangles(vector&lt;vector&lt;int&gt;&gt;&amp; rectangles) {
    int cur = 0;
    int ans = 0;
    for (const auto&amp; r : rectangles) {
      if (min(r[0], r[1]) &gt; cur) {
        cur = min(r[0], r[1]);
        ans = 0;
      }
      if (min(r[0], r[1]) == cur) ++ans;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1725-number-of-rectangles-that-can-form-the-largest-square/">花花酱 LeetCode 1725. Number Of Rectangles That Can Form The Largest Square</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-1725-number-of-rectangles-that-can-form-the-largest-square/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1620. Coordinate With Maximum Network Quality</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1620-coordinate-with-maximum-network-quality/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1620-coordinate-with-maximum-network-quality/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Oct 2020 20:29:03 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[gemoetry]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7516</guid>

					<description><![CDATA[<p>You are given an array of network towers&#160;towers&#160;and an integer&#160;radius, where&#160;towers[i] = [xi, yi, qi]&#160;denotes the&#160;ith&#160;network tower with location&#160;(xi, yi)&#160;and quality factor&#160;qi. All the coordinates&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1620-coordinate-with-maximum-network-quality/">花花酱 LeetCode 1620. Coordinate With Maximum Network Quality</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 are given an array of network towers&nbsp;<code>towers</code>&nbsp;and an integer&nbsp;<code>radius</code>, where&nbsp;<code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code>&nbsp;denotes the&nbsp;<code>i<sup>th</sup></code>&nbsp;network tower with location&nbsp;<code>(x<sub>i</sub>, y<sub>i</sub>)</code>&nbsp;and quality factor&nbsp;<code>q<sub>i</sub></code>. All the coordinates are&nbsp;<strong>integral coordinates</strong>&nbsp;on the X-Y plane, and the distance between two coordinates is the&nbsp;<strong>Euclidean distance</strong>.</p>



<p>The integer&nbsp;<code>radius</code>&nbsp;denotes the&nbsp;<strong>maximum distance</strong>&nbsp;in which the tower is&nbsp;<strong>reachable</strong>. The tower is&nbsp;<strong>reachable</strong>&nbsp;if the distance is less than or equal to&nbsp;<code>radius</code>. Outside that distance, the signal becomes garbled, and the tower is&nbsp;<strong>not reachable</strong>.</p>



<p>The signal quality of the&nbsp;<code>i<sup>th</sup></code>&nbsp;tower at a coordinate&nbsp;<code>(x, y)</code>&nbsp;is calculated with the formula&nbsp;<code>⌊q<sub>i</sub>&nbsp;/ (1 + d)⌋</code>, where&nbsp;<code>d</code>&nbsp;is the distance between the tower and the coordinate. The&nbsp;<strong>network quality</strong>&nbsp;at a coordinate is the sum of the signal qualities from all the&nbsp;<strong>reachable</strong>&nbsp;towers.</p>



<p>Return&nbsp;<em>the integral coordinate where the&nbsp;<strong>network quality</strong>&nbsp;is maximum</em>. If there are multiple coordinates with the same&nbsp;<strong>network quality</strong>, return&nbsp;<em>the lexicographically minimum coordinate</em>.</p>



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



<ul><li>A coordinate&nbsp;<code>(x1, y1)</code>&nbsp;is lexicographically smaller than&nbsp;<code>(x2, y2)</code>&nbsp;if either&nbsp;<code>x1 &lt; x2</code>&nbsp;or&nbsp;<code>x1 == x2</code>&nbsp;and&nbsp;<code>y1 &lt; y2</code>.</li><li><code>⌊val⌋</code>&nbsp;is the greatest integer less than or equal to&nbsp;<code>val</code>&nbsp;(the floor function).</li></ul>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/22/untitled-diagram.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> towers = [[1,2,5],[2,1,7],[3,1,9]], radius = 2
<strong>Output:</strong> [2,1]
<strong>Explanation: </strong>
At coordinate (2, 1) the total quality is 13
- Quality of 7 from (2, 1) results in ⌊7 / (1 + sqrt(0)⌋ = ⌊7⌋ = 7
- Quality of 5 from (1, 2) results in ⌊5 / (1 + sqrt(2)⌋ = ⌊2.07⌋ = 2
- Quality of 9 from (3, 1) results in ⌊9 / (1 + sqrt(1)⌋ = ⌊4.5⌋ = 4
No other coordinate has higher quality.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> towers = [[23,11,21]], radius = 9
<strong>Output:</strong> [23,11]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> towers = [[1,2,13],[2,1,7],[0,1,9]], radius = 2
<strong>Output:</strong> [1,2]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> towers = [[2,1,9],[0,1,9]], radius = 2
<strong>Output:</strong> [0,1]
<strong>Explanation: </strong>Both (0, 1) and (2, 1) are optimal in terms of quality but (0, 1) is lexicograpically minimal.
</pre>



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



<ul><li><code>1 &lt;= towers.length &lt;= 50</code></li><li><code>towers[i].length == 3</code></li><li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>&nbsp;&lt;= 50</code></li><li><code>1 &lt;= radius &lt;= 50</code></li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



<p>Try all possible coordinates from (0, 0) to (50, 50).</p>



<p>Time complexity: O(|X|*|Y|*t)<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  vector&lt;int&gt; bestCoordinate(vector&lt;vector&lt;int&gt;&gt;&amp; towers, int radius) {
    constexpr int n = 50;
    vector&lt;int&gt; ans;
    int max_q = 0;    
    for (int x = 0; x &lt;= n; ++x)
      for (int y = 0; y &lt;= n; ++y) {
        int q = 0;
        for (const auto&amp; tower : towers) {
          const int tx = tower[0], ty = tower[1];
          const float d = sqrt((x - tx) * (x - tx) + (y - ty) * (y - ty));
          if (d &gt; radius) continue;
          q += floor(tower[2] / (1 + d));
        }
        if (q &gt; max_q) {
          max_q = q;
          ans = {x, y};
        }
      }    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1620-coordinate-with-maximum-network-quality/">花花酱 LeetCode 1620. Coordinate With Maximum Network Quality</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-1620-coordinate-with-maximum-network-quality/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1610. Maximum Number of Visible Points</title>
		<link>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1610-maximum-number-of-visible-points/</link>
					<comments>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1610-maximum-number-of-visible-points/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 04 Oct 2020 19:26:47 +0000</pubDate>
				<category><![CDATA[Sliding Window]]></category>
		<category><![CDATA[gemoetry]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[sliding window]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7464</guid>

					<description><![CDATA[<p>You are given an array&#160;points, an integer&#160;angle, and your&#160;location, where&#160;location = [posx, posy]&#160;and&#160;points[i] = [xi, yi]&#160;both denote&#160;integral coordinates&#160;on the X-Y plane. Initially, you are facing&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-1610-maximum-number-of-visible-points/">花花酱 LeetCode 1610. Maximum Number of Visible Points</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1610. Maximum Number of Visible Points - 刷题找工作 EP360" width="500" height="281" src="https://www.youtube.com/embed/xn_VLwQnzvc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given an array&nbsp;<code>points</code>, an integer&nbsp;<code>angle</code>, and your&nbsp;<code>location</code>, where&nbsp;<code>location = [pos<sub>x</sub>, pos<sub>y</sub>]</code>&nbsp;and&nbsp;<code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>&nbsp;both denote&nbsp;<strong>integral coordinates</strong>&nbsp;on the X-Y plane.</p>



<p>Initially, you are facing directly east from your position. You&nbsp;<strong>cannot move</strong>&nbsp;from your position, but you can&nbsp;<strong>rotate</strong>. In other words,&nbsp;<code>pos<sub>x</sub></code>&nbsp;and&nbsp;<code>pos<sub>y</sub></code>&nbsp;cannot be changed. Your field of view in&nbsp;<strong>degrees</strong>&nbsp;is represented by&nbsp;<code>angle</code>, determining&nbsp;how wide you can see from any given view direction. Let&nbsp;<code>d</code>&nbsp;be the amount in degrees that you rotate counterclockwise. Then, your field of view is the&nbsp;<strong>inclusive</strong>&nbsp;range of angles&nbsp;<code>[d - angle/2, d + angle/2]</code>.</p>



<p>You can&nbsp;<strong>see</strong>&nbsp;some set of points if, for each point, the&nbsp;<strong>angle</strong>&nbsp;formed by the point, your position, and the immediate east direction from your position is&nbsp;<strong>in your field of view</strong>.</p>



<p>There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.</p>



<p>Return&nbsp;<em>the maximum number of points you can see</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/30/89a07e9b-00ab-4967-976a-c723b2aa8656.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The shaded region represents your field of view. All points can be made visible in your field of view, including [3,3] even though [2,2] is in front and in the same line of sight.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong> All points can be made visible in your field of view, including the one at your location.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/30/5010bfd3-86e6-465f-ac64-e9df941d2e49.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[1,0],[2,1]], angle = 13, location = [1,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can only see one of the two points, as shown above.
</pre>



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



<ul><li><code>1 &lt;= points.length &lt;= 10<sup>5</sup></code></li><li><code>points[i].length == 2</code></li><li><code>location.length == 2</code></li><li><code>0 &lt;= angle &lt; 360</code></li><li><code>0 &lt;= pos<sub>x</sub>, pos<sub>y</sub>, x<sub>i</sub>, y<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Sliding window</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/1610-ep360.png" alt="" class="wp-image-7471" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/1610-ep360.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/1610-ep360-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/1610-ep360-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Sort all the points by angle, duplicate the points with angle + 2*PI to deal with turn around case.</p>



<p>maintain a window [l, r] such that angle[r] &#8211; angle[l] &lt;= fov</p>



<p>Time complexity: O(nlogn)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int visiblePoints(vector&lt;vector&lt;int&gt;&gt;&amp; points, int angle, vector&lt;int&gt;&amp; o) {    
    int at_origin = 0;
    vector&lt;double&gt; ps;
    for (const auto&amp; p : points)
      if (p[0] == o[0] &amp;&amp; p[1] == o[1])
        ++at_origin;
      else
        ps.push_back(atan2(p[1] - o[1], p[0] - o[0]));
    sort(begin(ps), end(ps));
    const int n = ps.size();
    for (int i = 0; i &lt; n; ++i)
      ps.push_back(ps[i] + 2.0 * M_PI); // duplicate the array +2PI
    int l = 0;
    int ans = 0;
    double fov = angle * M_PI / 180.0;
    for (int r = 0; r &lt; ps.size(); ++r) {
      while (ps[r] - ps[l] &gt; fov) ++l;
      ans = max(ans, r - l + 1);
    }
    return ans + at_origin;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-1610-maximum-number-of-visible-points/">花花酱 LeetCode 1610. Maximum Number of Visible Points</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/sliding-window/leetcode-1610-maximum-number-of-visible-points/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1453. Maximum Number of Darts Inside of a Circular Dartboard</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 May 2020 05:22:56 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[angular sweep]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[gemoetry]]></category>
		<category><![CDATA[hard]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6770</guid>

					<description><![CDATA[<p>You have a very large square wall and a circular dartboard placed on the wall.&#160;You have been challenged to throw darts into the board blindfolded.&#160;Darts&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/">花花酱 LeetCode 1453. Maximum Number of Darts Inside of a Circular Dartboard</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 very large square wall and a circular dartboard placed on the wall.&nbsp;You have been challenged to throw darts into the board blindfolded.&nbsp;Darts thrown at the wall are represented as an array of&nbsp;<code>points</code>&nbsp;on a 2D plane.&nbsp;</p>



<p>Return&nbsp;the maximum number of points that are within or&nbsp;lie&nbsp;on&nbsp;<strong>any</strong>&nbsp;circular dartboard of radius&nbsp;<code>r</code>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/29/sample_1_1806.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[-2,0],[2,0],[0,2],[0,-2]], r = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Circle dartboard with center in (0,0) and radius = 2 contain all points.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/29/sample_2_1806.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]], r = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong> Circle dartboard with center in (0,4) and radius = 5 contain all points except the point (7,8).
</pre>



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



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



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



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



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



<ul><li><code>1 &lt;= points.length &lt;= 100</code></li><li><code>points[i].length == 2</code></li><li><code>-10^4 &lt;= points[i][0], points[i][1] &lt;= 10^4</code></li><li><code>1 &lt;= r &lt;= 5000</code></li></ul>



<h2><strong>Solution 1: Angular Sweep</strong></h2>



<p>See for more details: <a href="https://www.geeksforgeeks.org/angular-sweep-maximum-points-can-enclosed-circle-given-radius/">https://www.geeksforgeeks.org/angular-sweep-maximum-points-can-enclosed-circle-given-radius/</a></p>



<p>Time complexity: O(n^2*logn)<br>Space complexity: O(n^2)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
// Python-Like enumerate() In C++17
// http://reedbeta.com/blog/python-like-enumerate-in-cpp17/
template &lt;typename T,
          typename TIter = decltype(std::begin(std::declval&lt;T&gt;())),
          typename = decltype(std::end(std::declval&lt;T&gt;()))&gt;
constexpr auto enumerate(T &amp;&amp; iterable) {
  struct iterator {
    size_t i;
    TIter iter;
    bool operator != (const iterator &amp; other) const { return iter != other.iter; }
    void operator ++ () { ++i; ++iter; }
    auto operator * () const { return std::tie(i, *iter); }
  };
  struct iterable_wrapper {
    T iterable;
    auto begin() { return iterator{ 0, std::begin(iterable) }; }
    auto end() { return iterator{ 0, std::end(iterable) }; }
  };
  return iterable_wrapper{ std::forward&lt;T&gt;(iterable) };
}

class Solution {
public:
  int numPoints(vector&lt;vector&lt;int&gt;&gt;&amp; points, int r) {
    const int n = points.size();
    vector&lt;vector&lt;double&gt;&gt; d(n, vector&lt;double&gt;(n));
    
    for (const auto&amp; [i, pi] : enumerate(points))  
      for (const auto&amp; [j, pj] : enumerate(points))
        d[i][j] = d[j][i] = sqrt((pi[0] - pj[0]) * (pi[0] - pj[0]) 
                               + (pi[1] - pj[1]) * (pi[1] - pj[1]));
        
    int ans = 1;
    for (const auto&amp; [i, pi] : enumerate(points)) {    
      vector&lt;pair&lt;double, int&gt;&gt; angles; // {angle, event_type}
      for (const auto&amp; [j, pj] : enumerate(points)) {
        if (i != j &amp;&amp; d[i][j] &lt;= 2 * r) {
          const double a = atan2(pj[1] - pi[1], pj[0] - pi[0]);
          const double b = acos(d[i][j] / (2 * r));
          angles.emplace_back(a - b, -1); // entering
          angles.emplace_back(a + b, 1); // exiting
        }
      }
      // If angles are the same, entering points go first.
      sort(begin(angles), end(angles));
      int pts = 1;
      for (const auto&amp; [_, event] : angles)
        ans = max(ans, pts -= event);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/">花花酱 LeetCode 1453. Maximum Number of Darts Inside of a Circular Dartboard</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-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
