<?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>angular sweep Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/angular-sweep/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/angular-sweep/</link>
	<description></description>
	<lastBuildDate>Sun, 17 May 2020 06:30:40 +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>angular sweep Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/angular-sweep/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
