<?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>geometry Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/geometry/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/geometry/</link>
	<description></description>
	<lastBuildDate>Wed, 27 Apr 2022 15:57:45 +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>geometry Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/geometry/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2249. Count Lattice Points Inside a Circle</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-2249-count-lattice-points-inside-a-circle/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-2249-count-lattice-points-inside-a-circle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 27 Apr 2022 15:57:22 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9688</guid>

					<description><![CDATA[<p>Given a 2D integer array&#160;circles&#160;where&#160;circles[i] = [xi, yi, ri]&#160;represents the center&#160;(xi, yi)&#160;and radius&#160;ri&#160;of the&#160;ith&#160;circle drawn on a grid, return&#160;the&#160;number of lattice points&#160;that are present inside&#160;at&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-2249-count-lattice-points-inside-a-circle/">花花酱 LeetCode 2249. Count Lattice Points Inside a Circle</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 a 2D integer array&nbsp;<code>circles</code>&nbsp;where&nbsp;<code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code>&nbsp;represents the center&nbsp;<code>(x<sub>i</sub>, y<sub>i</sub>)</code>&nbsp;and radius&nbsp;<code>r<sub>i</sub></code>&nbsp;of the&nbsp;<code>i<sup>th</sup></code>&nbsp;circle drawn on a grid, return&nbsp;<em>the&nbsp;<strong>number of lattice points</strong>&nbsp;</em><em>that are present inside&nbsp;<strong>at least one</strong>&nbsp;circle</em>.</p>



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



<ul><li>A&nbsp;<strong>lattice point</strong>&nbsp;is a point with integer coordinates.</li><li>Points that lie&nbsp;<strong>on the circumference of a circle</strong>&nbsp;are also considered to be inside it.</li></ul>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/03/02/exa-11.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> circles = [[2,2,1]]
<strong>Output:</strong> 5
<strong>Explanation:</strong>
The figure above shows the given circle.
The lattice points present inside the circle are (1, 2), (2, 1), (2, 2), (2, 3), and (3, 2) and are shown in green.
Other points such as (1, 1) and (1, 3), which are shown in red, are not considered inside the circle.
Hence, the number of lattice points present inside at least one circle is 5.</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/03/02/exa-22.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> circles = [[2,2,2],[3,4,1]]
<strong>Output:</strong> 16
<strong>Explanation:</strong>
The figure above shows the given circles.
There are exactly 16 lattice points which are present inside at least one circle. 
Some of them are (0, 2), (2, 0), (2, 4), (3, 2), and (4, 4).
</pre>



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



<ul><li><code>1 &lt;= circles.length &lt;= 200</code></li><li><code>circles[i].length == 3</code></li><li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>&nbsp;&lt;= 100</code></li><li><code>1 &lt;= r<sub>i</sub>&nbsp;&lt;= min(x<sub>i</sub>, y<sub>i</sub>)</code></li></ul>



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



<p>Time complexity: O(m * m * n) = O(200 * 200 * 200)<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 countLatticePoints(vector&lt;vector&lt;int&gt;&gt;&amp; circles) {
    auto isIn = [](int u, int v, int x, int y, int r) {
      return (u - x) * (u - x) + (v - y) * (v - y) &lt;= r * r;
    };    
    int ans = 0;
    for (int u = 0; u &lt;= 200; ++u)
      for (int v = 0; v &lt;= 200; ++v) {
        bool found = false;
        for (const auto&amp; c : circles)
          if (isIn(u, v, c[0], c[1], c[2])) {
            found = true;
            break;
          }
        ans += found;
      }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-2249-count-lattice-points-inside-a-circle/">花花酱 LeetCode 2249. Count Lattice Points Inside a Circle</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-2249-count-lattice-points-inside-a-circle/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1232. Check If It Is a Straight Line</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1232-check-if-it-is-a-straight-line/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1232-check-if-it-is-a-straight-line/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 23 Dec 2021 21:15:32 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[eary]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9209</guid>

					<description><![CDATA[<p>You are given an array&#160;coordinates,&#160;coordinates[i] = [x, y], where&#160;[x, y]&#160;represents the coordinate of a point. Check if these points&#160;make a straight line in the XY&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1232-check-if-it-is-a-straight-line/">花花酱 LeetCode 1232. Check If It Is a Straight Line</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>coordinates</code>,&nbsp;<code>coordinates[i] = [x, y]</code>, where&nbsp;<code>[x, y]</code>&nbsp;represents the coordinate of a point. Check if these points&nbsp;make a straight line in the XY plane.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/15/untitled-diagram-2.jpg" alt=""/></figure>



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



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/09/untitled-diagram-1.jpg" alt=""/></figure>



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



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



<ul><li><code>2 &lt;=&nbsp;coordinates.length &lt;= 1000</code></li><li><code>coordinates[i].length == 2</code></li><li><code>-10^4 &lt;=&nbsp;coordinates[i][0],&nbsp;coordinates[i][1] &lt;= 10^4</code></li><li><code>coordinates</code>&nbsp;contains no duplicate point.</li></ul>



<h2><strong>Solution: Slope and Hashset</strong></h2>



<p>This is not a easy problem, a few corner cases:</p>



<ul><li>dx == 0</li><li>dy == 0</li><li>dx &lt; 0</li></ul>



<p>Basically we are counting (dx / gcd(dx, dy), dy / gcd(dx, dy)). We will have only ONE entry if all the points are on the same line.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1) w/ early exit.</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool checkStraightLine(vector&lt;vector&lt;int&gt;&gt;&amp; coordinates) {
    set&lt;pair&lt;int, int&gt;&gt; s;
    for (int i = 1; i &lt; coordinates.size(); ++i) {      
      int dx = coordinates[i][0] - coordinates[0][0];
      int dy = coordinates[i][1] - coordinates[0][1];      
      if (dy == 0)
        s.emplace(1, 0);
      else if (dx == 0)
        s.emplace(0, 1);
      else {
        if (dx &lt; 0) dx *= -1, dy *= -1;
        const int d = gcd(dx, dy);
        s.emplace(dx / d, dy / d);
      }
      if (s.size() &gt; 1) return false;
    }
    return true;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1232-check-if-it-is-a-straight-line/">花花酱 LeetCode 1232. Check If It Is a Straight Line</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-1232-check-if-it-is-a-straight-line/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2101. Detonate the Maximum Bombs</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-2101-detonate-the-maximum-bombs/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-2101-detonate-the-maximum-bombs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Dec 2021 21:04:14 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9139</guid>

					<description><![CDATA[<p>You are given a list of bombs. The&#160;range&#160;of a bomb is defined as the area where its effect can be felt. This area is in&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-2101-detonate-the-maximum-bombs/">花花酱 LeetCode 2101. Detonate the Maximum Bombs</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 a list of bombs. The&nbsp;<strong>range</strong>&nbsp;of a bomb is defined as the area where its effect can be felt. This area is in the shape of a&nbsp;<strong>circle</strong>&nbsp;with the center as the location of the bomb.</p>



<p>The bombs are represented by a&nbsp;<strong>0-indexed</strong>&nbsp;2D integer array&nbsp;<code>bombs</code>&nbsp;where&nbsp;<code>bombs[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code>.&nbsp;<code>x<sub>i</sub></code>&nbsp;and&nbsp;<code>y<sub>i</sub></code>&nbsp;denote the X-coordinate and Y-coordinate of the location of the&nbsp;<code>i<sup>th</sup></code>&nbsp;bomb, whereas&nbsp;<code>r<sub>i</sub></code>&nbsp;denotes the&nbsp;<strong>radius</strong>&nbsp;of its range.</p>



<p>You may choose to detonate a&nbsp;<strong>single</strong>&nbsp;bomb. When a bomb is detonated, it will detonate&nbsp;<strong>all bombs</strong>&nbsp;that lie in its range. These bombs will further detonate the bombs that lie in their ranges.</p>



<p>Given the list of&nbsp;<code>bombs</code>, return&nbsp;<em>the&nbsp;<strong>maximum</strong>&nbsp;number of bombs that can be detonated if you are allowed to detonate&nbsp;<strong>only one</strong>&nbsp;bomb</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> bombs = [[2,1,3],[6,1,4]]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The above figure shows the positions and ranges of the 2 bombs.
If we detonate the left bomb, the right bomb will not be affected.
But if we detonate the right bomb, both bombs will be detonated.
So the maximum bombs that can be detonated is max(1, 2) = 2.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> bombs = [[1,1,5],[10,10,5]]
<strong>Output:</strong> 1
<strong>Explanation:
</strong>Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/11/07/desmos-eg1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
<strong>Output:</strong> 5
<strong>Explanation:</strong>
The best bomb to detonate is bomb 0 because:
- Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0.
- Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2.
- Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3.
Thus all 5 bombs are detonated.
</pre>



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



<ul><li><code>1 &lt;= bombs.length&nbsp;&lt;= 100</code></li><li><code>bombs[i].length == 3</code></li><li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>&nbsp;&lt;= 10<sup>5</sup></code></li></ul>



<h2><strong>Solution: Simulation w/ BFS</strong></h2>



<p>Enumerate the bomb to detonate, and simulate the process using BFS.</p>



<p>Time complexity: O(n<sup>3</sup>)<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 maximumDetonation(vector&lt;vector&lt;int&gt;&gt;&amp; bombs) {
    const int n = bombs.size();    
    auto check = [&amp;](int i, int j) -&gt; bool {
      long x1 = bombs[i][0], y1 = bombs[i][1], r1 = bombs[i][2];
      long x2 = bombs[j][0], y2 = bombs[j][1], r2 = bombs[j][2];
      return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) &lt;= r1 * r1);          
    };
    int ans = 0;
    for (int s = 0; s &lt; n; ++s) {
      int count = 0;
      queue&lt;int&gt; q{{s}};
      vector&lt;int&gt; seen(n);
      seen[s] = 1;
      while (!q.empty()) {
        ++count;
        int i = q.front(); q.pop();
        for (int j = 0; j &lt; n; ++j)
          if (check(i, j) &amp;&amp; !seen[j]++)
            q.push(j);
      }
      ans = max(ans, count);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-2101-detonate-the-maximum-bombs/">花花酱 LeetCode 2101. Detonate the Maximum Bombs</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/simulation/leetcode-2101-detonate-the-maximum-bombs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1878. Get Biggest Three Rhombus Sums in a Grid</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1878-get-biggest-three-rhombus-sums-in-a-grid/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1878-get-biggest-three-rhombus-sums-in-a-grid/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 07 Aug 2021 06:55:56 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8506</guid>

					<description><![CDATA[<p>You are given an&#160;m x n&#160;integer matrix&#160;grid​​​. A&#160;rhombus sum&#160;is the sum of the elements that form&#160;the&#160;border&#160;of a regular rhombus shape in&#160;grid​​​. The rhombus must have&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1878-get-biggest-three-rhombus-sums-in-a-grid/">花花酱 LeetCode 1878. Get Biggest Three Rhombus Sums in a Grid</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&nbsp;<code>m x n</code>&nbsp;integer matrix&nbsp;<code>grid</code>​​​.</p>



<p>A&nbsp;<strong>rhombus sum</strong>&nbsp;is the sum of the elements that form&nbsp;<strong>the</strong>&nbsp;<strong>border</strong>&nbsp;of a regular rhombus shape in&nbsp;<code>grid</code>​​​. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus shapes with the corresponding colored cells that should be included in each&nbsp;<strong>rhombus sum</strong>:</p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/04/23/pc73-q4-desc-2.png" alt=""/></figure>



<p>Note that the rhombus can have an area of 0, which is depicted by the purple rhombus in the bottom right corner.</p>



<p>Return&nbsp;<em>the biggest three&nbsp;<strong>distinct rhombus sums</strong>&nbsp;in the&nbsp;</em><code>grid</code><em>&nbsp;in&nbsp;<strong>descending order</strong></em><em>. If there are less than three distinct values, return all of them</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/04/23/pc73-q4-ex1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]]
<strong>Output:</strong> [228,216,211]
<strong>Explanation:</strong> The rhombus shapes for the three biggest distinct rhombus sums are depicted above.
- Blue: 20 + 3 + 200 + 5 = 228
- Red: 200 + 2 + 10 + 4 = 216
- Green: 5 + 200 + 4 + 2 = 211
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/04/23/pc73-q4-ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[1,2,3],[4,5,6],[7,8,9]]
<strong>Output:</strong> [20,9,8]
<strong>Explanation:</strong> The rhombus shapes for the three biggest distinct rhombus sums are depicted above.
- Blue: 4 + 2 + 6 + 8 = 20
- Red: 9 (area 0 rhombus in the bottom right corner)
- Green: 8 (area 0 rhombus in the bottom middle)
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[7,7,7]]
<strong>Output:</strong> [7]
<strong>Explanation:</strong> All three possible rhombus sums are the same, so return [7].
</pre>



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



<ul><li><code>m == grid.length</code></li><li><code>n == grid[i].length</code></li><li><code>1 &lt;= m, n &lt;= 50</code></li><li><code>1 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li></ul>



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



<p>Just find all Rhombus&#8230;</p>



<p>Time complexity: O(mn*min(n,m)<sup>2</sup>)<br>Space complexity: O(mn*min(n,m)<sup>2</sup>)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; getBiggestThree(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    const int m = grid.size();
    const int n = grid[0].size();
    vector&lt;int&gt; ans;
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        ans.push_back(grid[i][j]);
    for (int a = 2; a &lt;= min(m, n); ++a)
      for (int cy = a - 1; cy + a &lt;= m; ++cy)
        for (int cx = a - 1; cx + a &lt;= n; ++cx) {
          int s = grid[cy][cx - a + 1]
                + grid[cy][cx + a - 1]
                + grid[cy + a - 1][cx]
                + grid[cy - a + 1][cx];
          for (int i = 1; i &lt; a - 1; ++i)
            s += grid[cy - i][cx - a + i + 1] 
               + grid[cy - i][cx + a - i - 1]
               + grid[cy + i][cx - a + i + 1] 
               + grid[cy + i][cx + a - i - 1];
          ans.push_back(s);          
        }
    sort(rbegin(ans), rend(ans));
    vector&lt;int&gt; output;
    for (int x : ans) {
      if (output.empty() || output.back() != x)
        output.push_back(x);
      if (output.size() == 3) break;
    }
    return output;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1878-get-biggest-three-rhombus-sums-in-a-grid/">花花酱 LeetCode 1878. Get Biggest Three Rhombus Sums in a Grid</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-1878-get-biggest-three-rhombus-sums-in-a-grid/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1828. Queries on Number of Points Inside a Circle</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Apr 2021 18:57:55 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8357</guid>

					<description><![CDATA[<p>You are given an array&#160;points&#160;where&#160;points[i] = [xi, yi]&#160;is the coordinates of the&#160;ith&#160;point on a 2D plane. Multiple points can have the&#160;same&#160;coordinates. You are also given&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/">花花酱 LeetCode 1828. Queries on Number of Points Inside a Circle</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>points</code>&nbsp;where&nbsp;<code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>&nbsp;is the coordinates of the&nbsp;<code>i<sup>th</sup></code>&nbsp;point on a 2D plane. Multiple points can have the&nbsp;<strong>same</strong>&nbsp;coordinates.</p>



<p>You are also given an array&nbsp;<code>queries</code>&nbsp;where&nbsp;<code>queries[j] = [x<sub>j</sub>, y<sub>j</sub>, r<sub>j</sub>]</code>&nbsp;describes a circle centered at&nbsp;<code>(x<sub>j</sub>, y<sub>j</sub>)</code>&nbsp;with a radius of&nbsp;<code>r<sub>j</sub></code>.</p>



<p>For each query&nbsp;<code>queries[j]</code>, compute the number of points&nbsp;<strong>inside</strong>&nbsp;the&nbsp;<code>j<sup>th</sup></code>&nbsp;circle. Points&nbsp;<strong>on the border</strong>&nbsp;of the circle are considered&nbsp;<strong>inside</strong>.</p>



<p>Return&nbsp;<em>an array&nbsp;</em><code>answer</code><em>, where&nbsp;</em><code>answer[j]</code><em>&nbsp;is the answer to the&nbsp;</em><code>j<sup>th</sup></code><em>&nbsp;query</em>.</p>



<p><strong>Example 1:</strong><img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-34-16.png"></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
<strong>Output:</strong> [3,2,2]
<strong>Explanation: </strong>The points and circles are shown above.
queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.
</pre>



<p><strong>Example 2:</strong><img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-42-07.png"></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
<strong>Output:</strong> [2,3,2,4]
<strong>Explanation: </strong>The points and circles are shown above.
queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.
</pre>



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



<ul><li><code>1 &lt;= points.length &lt;= 500</code></li><li><code>points[i].length == 2</code></li><li><code>0 &lt;= x<sub>​​​​​​i</sub>, y<sub>​​​​​​i</sub>&nbsp;&lt;= 500</code></li><li><code>1 &lt;= queries.length &lt;= 500</code></li><li><code>queries[j].length == 3</code></li><li><code>0 &lt;= x<sub>j</sub>, y<sub>j</sub>&nbsp;&lt;= 500</code></li><li><code>1 &lt;= r<sub>j</sub>&nbsp;&lt;= 500</code></li><li>All coordinates are integers.</li></ul>



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



<p>Time complexity: O(P * Q)<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:
  vector&lt;int&gt; countPoints(vector&lt;vector&lt;int&gt;&gt;&amp; points, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    vector&lt;int&gt; ans;
    ans.reserve(queries.size());
    for (const auto&amp; q : queries) {
      const int rs = q[2] * q[2];
      int cnt = 0;      
      for (const auto&amp; p : points)
        if ((q[0] - p[0]) * (q[0] - p[0]) + 
            (q[1] - p[1]) * (q[1] - p[1]) &lt;= rs)
          ++cnt;
      ans.push_back(cnt);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/">花花酱 LeetCode 1828. Queries on Number of Points Inside a Circle</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-1828-queries-on-number-of-points-inside-a-circle/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1779. Find Nearest Point That Has the Same X or Y Coordinate</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1779-find-nearest-point-that-has-the-same-x-or-y-coordinate/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1779-find-nearest-point-that-has-the-same-x-or-y-coordinate/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 06 Mar 2021 17:02:48 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[geometry]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8192</guid>

					<description><![CDATA[<p>You are given two integers,&#160;x&#160;and&#160;y, which represent your current location on a Cartesian grid:&#160;(x, y). You are also given an array&#160;points&#160;where each&#160;points[i] = [ai, bi]&#160;represents&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1779-find-nearest-point-that-has-the-same-x-or-y-coordinate/">花花酱 LeetCode 1779. Find Nearest Point That Has the Same X or Y Coordinate</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 two integers,&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>, which represent your current location on a Cartesian grid:&nbsp;<code>(x, y)</code>. You are also given an array&nbsp;<code>points</code>&nbsp;where each&nbsp;<code>points[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;represents that a point exists at&nbsp;<code>(a<sub>i</sub>, b<sub>i</sub>)</code>. A point is&nbsp;<strong>valid</strong>&nbsp;if it shares the same x-coordinate or the same y-coordinate as your location.</p>



<p>Return&nbsp;<em>the index&nbsp;<strong>(0-indexed)</strong>&nbsp;of the&nbsp;<strong>valid</strong>&nbsp;point with the smallest&nbsp;<strong>Manhattan distance</strong>&nbsp;from your current location</em>. If there are multiple, return&nbsp;<em>the valid point with the&nbsp;<strong>smallest</strong>&nbsp;index</em>. If there are no valid points, return&nbsp;<code>-1</code>.</p>



<p>The&nbsp;<strong>Manhattan distance</strong>&nbsp;between two points&nbsp;<code>(x<sub>1</sub>, y<sub>1</sub>)</code>&nbsp;and&nbsp;<code>(x<sub>2</sub>, y<sub>2</sub>)</code>&nbsp;is&nbsp;<code>abs(x<sub>1</sub>&nbsp;- x<sub>2</sub>) + abs(y<sub>1</sub>&nbsp;- y<sub>2</sub>)</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> x = 3, y = 4, points = [[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The answer is allowed to be on the same location as your current location.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> x = 3, y = 4, points = [[2,3]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are no valid points.</pre>



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



<ul><li><code>1 &lt;= points.length &lt;= 10<sup>4</sup></code></li><li><code>points[i].length == 2</code></li><li><code>1 &lt;= x, y, a<sub>i</sub>, b<sub>i</sub>&nbsp;&lt;= 10<sup>4</sup></code></li></ul>



<h2><strong>Solution: Brute Force</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 nearestValidPoint(int x, int y, vector&lt;vector&lt;int&gt;&gt;&amp; points) {
    int min_dist = INT_MAX;
    int ans = -1;
    for (int i = 0; i &lt; points.size(); ++i) {
      const int dx = abs(points[i][0] - x);
      const int dy = abs(points[i][1] - y);      
      if (dx * dy == 0 &amp;&amp; dx + dy &lt; min_dist) {
        min_dist = dx + dy;
        ans = i;
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1779-find-nearest-point-that-has-the-same-x-or-y-coordinate/">花花酱 LeetCode 1779. Find Nearest Point That Has the Same X or Y Coordinate</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-1779-find-nearest-point-that-has-the-same-x-or-y-coordinate/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 1515. Best Position for a Service Centre</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1515-best-position-for-a-service-centre/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1515-best-position-for-a-service-centre/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 30 Nov 2020 09:02:48 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[hard]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7751</guid>

					<description><![CDATA[<p>A delivery company wants to build a new service centre in a new city. The company knows the positions of all the customers in this&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1515-best-position-for-a-service-centre/">花花酱 LeetCode 1515. Best Position for a Service Centre</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>A delivery company wants to build a new service centre in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the new centre in a position such that&nbsp;<strong>the sum of the euclidean distances to all customers is minimum</strong>.</p>



<p>Given an array&nbsp;<code>positions</code>&nbsp;where&nbsp;<code>positions[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>&nbsp;is the position of the&nbsp;<code>ith</code>&nbsp;customer on the map, return&nbsp;<em>the minimum sum of the euclidean distances</em>&nbsp;to all customers.</p>



<p>In other words, you need to choose the position of the service centre&nbsp;<code>[x<sub>centre</sub>, y<sub>centre</sub>]</code>&nbsp;such that the following formula is minimized:</p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/06/25/q4_edited.jpg" alt=""/></figure>



<p>Answers within&nbsp;<code>10^-5</code>&nbsp;of the actual value will be accepted.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/06/25/q4_e1.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> positions = [[0,1],[1,0],[1,2],[2,1]]
<strong>Output:</strong> 4.00000
<strong>Explanation:</strong> As shown, you can see that choosing [x<sub>centre</sub>, y<sub>centre</sub>] = [1, 1] will make the distance to each customer = 1, the sum of all distances is 4 which is the minimum possible we can achieve.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/06/25/q4_e3.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> positions = [[1,1],[3,3]]
<strong>Output:</strong> 2.82843
<strong>Explanation:</strong> The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843
</pre>



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



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> positions = [[1,1],[0,0],[2,0]]
<strong>Output:</strong> 2.73205
<strong>Explanation:</strong> At the first glance, you may think that locating the centre at [1, 0] will achieve the minimum sum, but locating it at [1, 0] will make the sum of distances = 3.
Try to locate the centre at [1.0, 0.5773502711] you will see that the sum of distances is 2.73205.
Be careful with the precision!
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> positions = [[0,1],[3,2],[4,5],[7,6],[8,9],[11,1],[2,12]]
<strong>Output:</strong> 32.94036
<strong>Explanation:</strong> You can use [4.3460852395, 4.9813795505] as the position of the centre.
</pre>



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



<ul><li><code>1 &lt;=&nbsp;positions.length &lt;= 50</code></li><li><code>positions[i].length == 2</code></li><li><code>0 &lt;=&nbsp;positions[i][0],&nbsp;positions[i][1] &lt;= 100</code></li></ul>



<h2><strong>Solution: Weiszfeld&#8217;s algorithm</strong></h2>



<p>Use Weiszfeld&#8217;s algorithm to compute geometric median of the samples.</p>



<p>Time complexity: O(f(epsilon) * O)<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">template&lt;typename T1, typename T2&gt;
double dist(const vector&lt;T1&gt;&amp; a, const vector&lt;T2&gt;&amp; b) {
  return sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]));
};

class Solution {
public:
  double getMinDistSum(vector&lt;vector&lt;int&gt;&gt;&amp; positions) {    
    constexpr double kDelta = 1e-7;
    constexpr double kEpsilon = 1e-15;
    vector&lt;double&gt; c{-1, -1};
    double ans = 0;
    while (true) {
      ans = 0;
      double nx = 0, ny = 0;
      double denominator = 0;
      for (const auto&amp; p : positions) {
        double d = dist(c, p) + kEpsilon;
        ans += d;
        nx += p[0] / d;
        ny += p[1] / d;
        denominator += 1.0 / d;
      }
      vector&lt;double&gt; t{nx / denominator, ny / denominator};      
      if (dist(c, t) &lt; kDelta) return ans;
      c = t;
    }
    return -1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1515-best-position-for-a-service-centre/">花花酱 LeetCode 1515. Best Position for a Service Centre</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-1515-best-position-for-a-service-centre/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1637. Widest Vertical Area Between Two Points Containing No Points</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1637-widest-vertical-area-between-two-points-containing-no-points/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1637-widest-vertical-area-between-two-points-containing-no-points/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 31 Oct 2020 20:29:59 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7582</guid>

					<description><![CDATA[<p>Given&#160;n&#160;points&#160;on a 2D plane where&#160;points[i] = [xi, yi], Return&#160;the&#160;widest vertical area&#160;between two points such that no points are inside the area. A&#160;vertical area&#160;is an area&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1637-widest-vertical-area-between-two-points-containing-no-points/">花花酱 LeetCode 1637. Widest Vertical Area Between Two Points Containing No 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[
<p>Given&nbsp;<code>n</code>&nbsp;<code>points</code>&nbsp;on a 2D plane where&nbsp;<code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>, Return<em>&nbsp;the&nbsp;<strong>widest vertical area</strong>&nbsp;between two points such that no points are inside the area.</em></p>



<p>A&nbsp;<strong>vertical area</strong>&nbsp;is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The&nbsp;<strong>widest vertical area</strong>&nbsp;is the one with the maximum width.</p>



<p>Note that points&nbsp;<strong>on the edge</strong>&nbsp;of a vertical area&nbsp;<strong>are not</strong>&nbsp;considered included in the area.</p>



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



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



<p>​</p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[8,7],[9,9],[7,4],[9,7]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Both the red and the blue area are optimal.
</pre>



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



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



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



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



<h2><strong>Solution: Sort by x coordinates</strong></h2>



<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">class Solution {
public:
  int maxWidthOfVerticalArea(vector&lt;vector&lt;int&gt;&gt;&amp; points) {
    sort(begin(points), end(points), [](const auto&amp; p1, const auto&amp; p2) {
      return p1[0] != p2[0] ? p1[0] &lt; p2[0] : p1[1] &lt; p2[1];
    });
    int ans = 0;
    for (int i = 1; i &lt; points.size(); ++i)
      ans = max(ans, points[i][0] - points[i - 1][0]);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1637-widest-vertical-area-between-two-points-containing-no-points/">花花酱 LeetCode 1637. Widest Vertical Area Between Two Points Containing No 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/geometry/leetcode-1637-widest-vertical-area-between-two-points-containing-no-points/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1476. Subrectangle Queries</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1476-subrectangle-queries/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1476-subrectangle-queries/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 14 Jun 2020 03:41:08 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[update]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6909</guid>

					<description><![CDATA[<p>Implement the class&#160;SubrectangleQueries&#160;which receives a&#160;rows x cols&#160;rectangle as a matrix of integers in the constructor and supports two methods: 1.&#160;updateSubrectangle(int row1, int col1, int row2,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1476-subrectangle-queries/">花花酱 LeetCode 1476. Subrectangle Queries</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>Implement the class&nbsp;<code>SubrectangleQueries</code>&nbsp;which receives a&nbsp;<code>rows x cols</code>&nbsp;rectangle as a matrix of integers in the constructor and supports two methods:</p>



<p>1.<code>&nbsp;updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)</code></p>



<ul><li>Updates all values with&nbsp;<code>newValue</code>&nbsp;in the subrectangle whose upper left coordinate is&nbsp;<code>(row1,col1)</code>&nbsp;and bottom right coordinate is&nbsp;<code>(row2,col2)</code>.</li></ul>



<p>2.<code>&nbsp;getValue(int row, int col)</code></p>



<ul><li>Returns the current value of the coordinate&nbsp;<code>(row,col)</code>&nbsp;from&nbsp;the rectangle.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"]
[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]
<strong>Output</strong>
</pre>


<p>[null,1,null,5,5,null,10,5]</p>



<p><strong>Explanation</strong>
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);  
// The initial rectangle (4&#215;3) looks like:
// 1 2 1
// 4 3 4
// 3 2 1
// 1 1 1
subrectangleQueries.getValue(0, 2); // return 1
subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5);
// After this update the rectangle looks like:
// 5 5 5
// 5 5 5
// 5 5 5
// 5 5 5 
subrectangleQueries.getValue(0, 2); // return 5
subrectangleQueries.getValue(3, 1); // return 5
subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10);
// After this update the rectangle looks like:
// 5   5   5
// 5   5   5
// 5   5   5
// 10  10  10 
subrectangleQueries.getValue(3, 1); // return 10
subrectangleQueries.getValue(0, 2); // return 5
</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"]
[[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]
<strong>Output</strong>
</pre>


<p>[null,1,null,100,100,null,20]</p>



<p><strong>Explanation</strong>
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]);
subrectangleQueries.getValue(0, 0); // return 1
subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100);
subrectangleQueries.getValue(0, 0); // return 100
subrectangleQueries.getValue(2, 2); // return 100
subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20);
subrectangleQueries.getValue(2, 2); // return 20
</p>



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



<ul><li>There will be at most&nbsp;<code>500</code>&nbsp;operations considering both methods:&nbsp;<code>updateSubrectangle</code>&nbsp;and&nbsp;<code>getValue</code>.</li><li><code>1 &lt;= rows, cols &lt;= 100</code></li><li><code>rows ==&nbsp;rectangle.length</code></li><li><code>cols == rectangle[i].length</code></li><li><code>0 &lt;= row1 &lt;= row2 &lt; rows</code></li><li><code>0 &lt;= col1 &lt;= col2 &lt; cols</code></li><li><code>1 &lt;= newValue, rectangle[i][j] &lt;= 10^9</code></li><li><code>0 &lt;= row &lt; rows</code></li><li><code>0 &lt;= col &lt; cols</code></li></ul>



<h2><strong>Solution 1: Simulation</strong></h2>



<p>Update the matrix values.</p>



<p>Time complexity: <br>Update: O(m*n), where m*n is the area of the sub-rectangle.<br>Query: O(1)</p>



<p>Space complexity: O(rows*cols) </p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class SubrectangleQueries {
public:
  SubrectangleQueries(vector&lt;vector&lt;int&gt;&gt;&amp; rectangle): 
    m_(rectangle) {}

  void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
    for (int i = row1; i &lt;= row2; ++i)
      for (int j = col1; j &lt;= col2; ++j)
        m_[i][j] = newValue;
  }

  int getValue(int row, int col) {
    return m_[row][col];
  }
private:
  vector&lt;vector&lt;int&gt;&gt; m_;
};</pre>
</div></div>



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



<p>For each update remember the region and value.</p>



<p>For each query, find the newest updates that covers the query point. If not found, return the original value in the matrix.</p>



<p>Time complexity:<br>Update: O(1)<br>Query: O(|U|), where |U| is the number of updates so far.</p>



<p>Space complexity: O(|U|)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class SubrectangleQueries {
public:
  SubrectangleQueries(vector&lt;vector&lt;int&gt;&gt;&amp; rectangle): 
    m_(rectangle) {}

  void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
    updates_.push_front({row1, col1, row2, col2, newValue});
  }

  int getValue(int row, int col) {
    for (const auto&amp; u : updates_)
      if (row &gt;= u[0] &amp;&amp; row &lt;= u[2] &amp;&amp; col &gt;= u[1] &amp;&amp; col &lt;= u[3])
        return u[4];
    return m_[row][col];
  }
private:
  const vector&lt;vector&lt;int&gt;&gt;&amp; m_;
  deque&lt;vector&lt;int&gt;&gt; updates_;  
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1476-subrectangle-queries/">花花酱 LeetCode 1476. Subrectangle Queries</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-1476-subrectangle-queries/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 May 2020 16:49:59 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[diff]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6857</guid>

					<description><![CDATA[<p>Given a rectangular cake with height&#160;h&#160;and width&#160;w, and two arrays of integers&#160;horizontalCuts&#160;and&#160;verticalCuts&#160;where&#160;horizontalCuts[i]&#160;is the distance from the top of the rectangular cake to the&#160;ith&#160;horizontal cut&#160;and similarly,&#160;verticalCuts[j]&#160;is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/">花花酱 LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts</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 a rectangular cake with height&nbsp;<code>h</code>&nbsp;and width&nbsp;<code>w</code>, and two arrays of integers&nbsp;<code>horizontalCuts</code>&nbsp;and&nbsp;<code>verticalCuts</code>&nbsp;where&nbsp;<code>horizontalCuts[i]</code>&nbsp;is the distance from the top of the rectangular cake to the&nbsp;<code>ith</code>&nbsp;horizontal cut&nbsp;and similarly,&nbsp;<code>verticalCuts[j]</code>&nbsp;is the distance from the&nbsp;left of the rectangular cake to the&nbsp;<code>jth</code>&nbsp;vertical cut.</p>



<p><em>Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays&nbsp;<code>horizontalCuts</code>&nbsp;and&nbsp;<code>verticalCuts</code>.&nbsp;</em>Since the answer can be a huge number, return this modulo 10^9 + 7.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/05/14/leetcode_max_area_2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
<strong>Output:</strong> 4 
<strong>Explanation:</strong> The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/05/14/leetcode_max_area_3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
<strong>Output:</strong> 9
</pre>



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



<ul><li><code>2 &lt;= h,&nbsp;w &lt;= 10^9</code></li><li><code>1 &lt;=&nbsp;horizontalCuts.length &lt;&nbsp;min(h, 10^5)</code></li><li><code>1 &lt;=&nbsp;verticalCuts.length &lt; min(w, 10^5)</code></li><li><code>1 &lt;=&nbsp;horizontalCuts[i] &lt; h</code></li><li><code>1 &lt;=&nbsp;verticalCuts[i] &lt; w</code></li><li>It is guaranteed that all elements in&nbsp;<code>horizontalCuts</code>&nbsp;are distinct.</li><li>It is guaranteed that all elements in&nbsp;<code>verticalCuts</code>&nbsp;are distinct.</li></ul>



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



<p>Find the max gap between vertical cuts mx and max gap between horizontal cuts my. ans = mx * my</p>



<p>Time complexity: O(nlogn)<br>Space complexity: O(1) if sort in place otherweise 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 maxArea(int h, int w, vector&lt;int&gt;&amp; horizontalCuts, vector&lt;int&gt;&amp; verticalCuts) {
    constexpr int kMod = 1e9 + 7;
    sort(begin(verticalCuts), end(verticalCuts));
    sort(begin(horizontalCuts), end(horizontalCuts));
    int mx = max(verticalCuts[0], w - verticalCuts.back());
    int my = max(horizontalCuts[0], h - horizontalCuts.back());
    for (int i = 1; i &lt; verticalCuts.size(); ++i)
      mx = max(mx, verticalCuts[i] - verticalCuts[i - 1]);
    for (int i = 1; i &lt; horizontalCuts.size(); ++i)
      my = max(my, horizontalCuts[i] - horizontalCuts[i - 1]);               
    return static_cast&lt;long&gt;(mx) * my % kMod;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/">花花酱 LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts</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-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1401. Circle and Rectangle Overlapping</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 03:49:18 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[inside]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rectangle]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6569</guid>

					<description><![CDATA[<p>Given a circle represented as (radius,&#160;x_center,&#160;y_center)&#160;and an axis-aligned rectangle represented as (x1,&#160;y1,&#160;x2,&#160;y2),&#160;where (x1,&#160;y1) are the coordinates of the bottom-left corner, and (x2,&#160;y2) are the coordinates&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/">花花酱 LeetCode 1401. Circle and Rectangle Overlapping</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 a circle represented as (<code>radius</code>,&nbsp;<code>x_center</code>,&nbsp;<code>y_center</code>)&nbsp;and an axis-aligned rectangle represented as (<code>x1</code>,&nbsp;<code>y1</code>,&nbsp;<code>x2</code>,&nbsp;<code>y2</code>),&nbsp;where (<code>x1</code>,&nbsp;<code>y1</code>) are the coordinates of the bottom-left corner, and (<code>x2</code>,&nbsp;<code>y2</code>) are the coordinates of the top-right corner of the&nbsp;rectangle.</p>



<p>Return True if the circle and rectangle are overlapped otherwise return False.</p>



<p>In other words, check if there are&nbsp;<strong>any&nbsp;</strong>point&nbsp;(xi, yi) such that belongs to the circle and the rectangle at the same time.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/02/20/sample_4_1728.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> radius = 1, x_center = 0, y_center = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> Circle and rectangle share the point (1,0) 
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/02/20/sample_2_1728.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> radius = 1, x_center = 0, y_center = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1
<strong>Output:</strong> true
</pre>



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> radius = 1, x_center = 1, y_center = 1, x1 = -3, y1 = -3, x2 = 3, y2 = 3
<strong>Output:</strong> true
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> radius = 1, x_center = 1, y_center = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1
<strong>Output:</strong> false
</pre>



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



<ul><li><code>1 &lt;= radius &lt;= 2000</code></li><li><code>-10^4 &lt;= x_center, y_center, x1, y1, x2, y2 &lt;= 10^4</code></li><li><code>x1 &lt; x2</code></li><li><code>y1 &lt; y2</code></li></ul>



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



<p>Find the shortest distance from the center to the rectangle, return dist &lt;= radius.</p>



<p>Time complexity: O(1)<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:
  bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
    int dx = x_center - max(x1, min(x2, x_center));
    int dy = y_center - max(y1, min(y2, y_center));
    return dx * dx + dy * dy &lt;= radius * radius;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/">花花酱 LeetCode 1401. Circle and Rectangle Overlapping</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-1401-circle-and-rectangle-overlapping/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1386. Cinema Seat Allocation</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1386-cinema-seat-allocation/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1386-cinema-seat-allocation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Mar 2020 20:34:07 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[binary mask]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6533</guid>

					<description><![CDATA[<p>A cinema&#160;has&#160;n&#160;rows of seats, numbered from 1 to&#160;n&#160;and there are ten&#160;seats in each row, labelled from 1&#160;to 10&#160;as shown in the figure above. Given the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1386-cinema-seat-allocation/">花花酱 LeetCode 1386. Cinema Seat Allocation</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-image"><img src="https://assets.leetcode.com/uploads/2020/02/14/cinema_seats_1.png" alt=""/></figure>



<p>A cinema&nbsp;has&nbsp;<code>n</code>&nbsp;rows of seats, numbered from 1 to&nbsp;<code>n</code>&nbsp;and there are ten&nbsp;seats in each row, labelled from 1&nbsp;to 10&nbsp;as shown in the figure above.</p>



<p>Given the array&nbsp;<code>reservedSeats</code>&nbsp;containing the numbers of seats already reserved, for example,&nbsp;<code>reservedSeats[i]=[3,8]</code>&nbsp;means the seat located in row&nbsp;<strong>3</strong>&nbsp;and labelled with&nbsp;<strong>8</strong>&nbsp;is already reserved.&nbsp;</p>



<p><em>Return the maximum number of four-person families you can allocate on the cinema&nbsp;seats.</em>&nbsp;A four-person family occupies fours seats&nbsp;<strong>in one row</strong>, that are&nbsp;<strong>next to each other</strong>. Seats across an aisle (such as [3,3]&nbsp;and [3,4]) are not considered to be next to each other, however, It is permissible for the four-person family to be separated by an aisle, but in that case,&nbsp;<strong>exactly two people</strong>&nbsp;have to sit on each side of the aisle.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/02/14/cinema_seats_3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The figure above shows the optimal allocation for four families, where seats mark with blue are already reserved and contiguous seats mark with orange are for one family.&nbsp;
</pre>



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



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



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



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



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



<ul><li><code>1 &lt;= n &lt;= 10^9</code></li><li><code>1 &lt;=&nbsp;reservedSeats.length &lt;= min(10*n, 10^4)</code></li><li><code>reservedSeats[i].length == 2</code></li><li><code>1&nbsp;&lt;=&nbsp;reservedSeats[i][0] &lt;= n</code></li><li><code>1 &lt;=&nbsp;reservedSeats[i][1] &lt;= 10</code></li><li>All&nbsp;<code>reservedSeats[i]</code>&nbsp;are distinct.</li></ul>



<h2><strong>Solution: HashTable + Greedy</strong></h2>



<p>if both seat[2~5] seat[6~9] are empty, seat two groups.<br>if any of seat[2~5] seat[4~7] seat[6~9] is empty seat one group.<br>if there is no one sit in a row, seat two groups.</p>



<p>Time complexity: O(|reservedSeats|)<br>Space complexity: O(|rows|)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxNumberOfFamilies(int n, vector&lt;vector&lt;int&gt;&gt;&amp; reservedSeats) {
    unordered_map&lt;int, int&gt; rows;
    for (auto&amp; seat : reservedSeats)
      rows[seat[0]] |= 1 &lt;&lt; (seat[1] - 1);
    
    int ans = (n - rows.size()) * 2;
    
    for (const auto&amp; [idx, row] : rows) {
      int s2 = row &amp; 0b0000011110;
      int s4 = row &amp; 0b0001111000;
      int s6 = row &amp; 0b0111100000;
      if (s2 == 0 &amp;&amp; s6 == 0)
        ans += 2;
      else if (s2 == 0 || s4 == 0 || s6 == 0)
        ans += 1;
    }
    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1386-cinema-seat-allocation/">花花酱 LeetCode 1386. Cinema Seat Allocation</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/hashtable/leetcode-1386-cinema-seat-allocation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1326. Minimum Number of Taps to Open to Water a Garden</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1326-minimum-number-of-taps-to-open-to-water-a-garden/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1326-minimum-number-of-taps-to-open-to-water-a-garden/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 20 Jan 2020 00:48:50 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[O(nlogn)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6112</guid>

					<description><![CDATA[<p>There is a one-dimensional garden on the x-axis. The garden starts at the point&#160;0&#160;and ends at the point&#160;n. (i.e The length of the garden is&#160;n).&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1326-minimum-number-of-taps-to-open-to-water-a-garden/">花花酱 LeetCode 1326. Minimum Number of Taps to Open to Water a Garden</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-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1326. Minimum Number of Taps to Open to Water a Garden - 刷题找工作 EP301" width="500" height="375" src="https://www.youtube.com/embed/G88X89Eo2C0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>There is a one-dimensional garden on the x-axis. The garden starts at the point&nbsp;<code>0</code>&nbsp;and ends at the point&nbsp;<code>n</code>. (i.e The length of the garden is&nbsp;<code>n</code>).</p>



<p>There are&nbsp;<code>n + 1</code>&nbsp;taps located&nbsp;at points&nbsp;<code>[0, 1, ..., n]</code>&nbsp;in the garden.</p>



<p>Given an integer&nbsp;<code>n</code>&nbsp;and an integer array&nbsp;<code>ranges</code>&nbsp;of length&nbsp;<code>n + 1</code>&nbsp;where&nbsp;<code>ranges[i]</code>&nbsp;(0-indexed) means the&nbsp;<code>i-th</code>&nbsp;tap can water the area&nbsp;<code>[i - ranges[i], i + ranges[i]]</code>&nbsp;if it was open.</p>



<p>Return&nbsp;<em>the minimum number of taps</em>&nbsp;that should be open to water the whole garden, If the garden cannot be watered return&nbsp;<strong>-1</strong>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/01/16/1685_example_1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, ranges = [3,4,1,1,0,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, ranges = [0,0,0,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Even if you activate all the four taps you cannot water the whole garden.
</pre>



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



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 8, ranges = [4,0,0,0,0,0,0,0,4]
<strong>Output:</strong> 2
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 8, ranges = [4,0,0,0,4,0,0,0,4]
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 10^4</code></li><li><code>ranges.length == n + 1</code></li><li><code>0 &lt;= ranges[i] &lt;= 100</code></li></ul>



<h2><strong>Solution 1: Greedy</strong></h2>



<p>Reduce to <a href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-131-1021-1022-1023-1024/">1024. Video Stitching</a></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 minTaps(int n, vector&lt;int&gt;&amp; ranges) {
    vector&lt;pair&lt;int, int&gt;&gt; t;
    // O(n) reduction
    for (int i = 0; i &lt;= n; ++i)
      t.emplace_back(max(0, i - ranges[i]), 
                     min(i + ranges[i], n));
    
    // 1024. Video Stiching
    sort(begin(t), end(t));
    int ans = 0;
    int i = 0;
    int l = 0;
    int e = 0;
    while (e &lt; n) {
      // Extend to the right most w.r.t t[i].first &lt;= l
      while (i &lt;= n &amp;&amp; t[i].first &lt;= l)
        e = max(e, t[i++].second);
      if (l == e) return -1; // Can not extend
      l = e;
      ++ans;
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Greedy</strong></h2>



<p>Reduce to <a href="https://zxi.mytechroad.com/blog/greedy/leetcode-45-jump-game-ii/">45. Jump Game II</a></p>



<p>Time complexity: O(n)<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 minTaps(int n, vector&lt;int&gt;&amp; ranges) {
    vector&lt;int&gt; nums(ranges.size());
    for (int i = 0; i &lt;= n; ++i) {
      int s = max(0, i - ranges[i]);      
      nums[s] = max(nums[s], i + ranges[i]);
    }    
    // 45. Jump Game II
    int steps = 0;
    int l = 0;
    int e = 0;
    for (int i = 0; i &lt;= n; ++i) {
      if (i &gt; e) return -1;
      if (i &gt; l) { ++steps; l = e; }
      e = max(e, nums[i]);
    }
    return steps;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1326-minimum-number-of-taps-to-open-to-water-a-garden/">花花酱 LeetCode 1326. Minimum Number of Taps to Open to Water a Garden</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-1326-minimum-number-of-taps-to-open-to-water-a-garden/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1272. Remove Interval</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1272-remove-interval/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1272-remove-interval/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 01 Dec 2019 04:50:58 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[interval]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5893</guid>

					<description><![CDATA[<p>Given a&#160;sorted&#160;list of disjoint&#160;intervals, each interval&#160;intervals[i] = [a, b]&#160;represents the set of real numbers&#160;x&#160;such that&#160;a &#60;= x &#60; b. We remove the intersections between any&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1272-remove-interval/">花花酱 LeetCode 1272. Remove Interval</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 a&nbsp;<strong>sorted</strong>&nbsp;list of disjoint&nbsp;<code>intervals</code>, each interval&nbsp;<code>intervals[i] = [a, b]</code>&nbsp;represents the set of real numbers&nbsp;<code>x</code>&nbsp;such that&nbsp;<code>a &lt;= x &lt; b</code>.</p>



<p>We remove the intersections between any interval in&nbsp;<code>intervals</code>&nbsp;and the interval&nbsp;<code>toBeRemoved</code>.</p>



<p>Return a&nbsp;<strong>sorted</strong>&nbsp;list of&nbsp;<code>intervals</code>&nbsp;after all such removals.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]
<strong>Output:</strong> [[0,1],[6,7]]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> intervals = [[0,5]], toBeRemoved = [2,3]
<strong>Output:</strong> [[0,2],[3,5]]
</pre>



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



<ul><li><code>1 &lt;= intervals.length &lt;= 10^4</code></li><li><code>-10^9 &lt;= intervals[i][0] &lt; intervals[i][1] &lt;= 10^9</code></li></ul>



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



<p>Time complexity: O(n)<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:
  vector&lt;vector&lt;int&gt;&gt; removeInterval(vector&lt;vector&lt;int&gt;&gt;&amp; intervals, vector&lt;int&gt;&amp; r) {
    vector&lt;vector&lt;int&gt;&gt; ans;
    for (const auto&amp; i : intervals)
      // Does not intersect
      if (i[1] &lt;= r[0] || i[0] &gt;= r[1])
        ans.push_back(i);
      else {
        // i starts first
        if (i[0] &lt; r[0]) 
          ans.push_back({i[0], r[0]});
        // i ends later
        if (i[1] &gt; r[1])
          ans.push_back({r[1], i[1]});        
      }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1272-remove-interval/">花花酱 LeetCode 1272. Remove Interval</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-1272-remove-interval/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
