<?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/category/geometry/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/category/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/category/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 1895. Largest Magic Square</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1895-largest-magic-square/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1895-largest-magic-square/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 10 Aug 2021 03:24:57 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8555</guid>

					<description><![CDATA[<p>A&#160;k x k&#160;magic square&#160;is a&#160;k x k&#160;grid filled with integers such that every row sum, every column sum, and both diagonal sums are&#160;all equal. The&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1895-largest-magic-square/">花花酱 LeetCode 1895. Largest Magic 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>A&nbsp;<code>k x k</code>&nbsp;<strong>magic square</strong>&nbsp;is a&nbsp;<code>k x k</code>&nbsp;grid filled with integers such that every row sum, every column sum, and both diagonal sums are&nbsp;<strong>all equal</strong>. The integers in the magic square&nbsp;<strong>do not have to be distinct</strong>. Every&nbsp;<code>1 x 1</code>&nbsp;grid is trivially a&nbsp;<strong>magic square</strong>.</p>



<p>Given an&nbsp;<code>m x n</code>&nbsp;integer&nbsp;<code>grid</code>, return&nbsp;<em>the&nbsp;<strong>size</strong>&nbsp;(i.e., the side length&nbsp;</em><code>k</code><em>) of the&nbsp;<strong>largest magic square</strong>&nbsp;that can be found within this grid</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare-grid.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[7,1,4,5,6],[2,5,1,6,4],[1,5,4,3,2],[1,2,7,3,4]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The largest magic square has a size of 3.
Every row sum, column sum, and diagonal sum of this magic square is equal to 12.
- Row sums: 5+1+6 = 5+4+3 = 2+7+3 = 12
- Column sums: 5+5+2 = 1+4+7 = 6+3+3 = 12
- Diagonal sums: 5+4+3 = 6+4+2 = 12
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare2-grid.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[5,1,3,1],[9,3,3,1],[1,3,3,8]]
<strong>Output:</strong> 2
</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>6</sup></code></li></ul>



<h2><strong>Solution: Brute Force w/ Prefix Sum</strong></h2>



<p>Compute the prefix sum for each row and each column.</p>



<p>And check all possible squares.</p>



<p>Time complexity: O(m*n*min(m,n)<sup>2</sup>)<br>Space complexity: O(m*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 largestMagicSquare(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    const int m = grid.size();
    const int n = grid[0].size();
    vector&lt;vector&lt;int&gt;&gt; rows(m, vector&lt;int&gt;(n + 1));
    vector&lt;vector&lt;int&gt;&gt; cols(n, vector&lt;int&gt;(m + 1));
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j) {
        rows[i][j + 1] = rows[i][j] + grid[i][j];
        cols[j][i + 1] = cols[j][i] + grid[i][j];
      }
    for (int k = min(m, n); k &gt;= 2; --k)
      for (int i = 0; i + k &lt;= m; ++i)
        for (int j = 0; j + k &lt;= n; ++j) {
          vector&lt;int&gt; s;
          for (int r = 0; r &lt; k; ++r) 
            s.push_back(rows[i + r][j + k] - rows[i + r][j]);
          for (int c = 0; c &lt; k; ++c)
            s.push_back(cols[j + c][i + k] - cols[j + c][i]);
          int d1 = 0;
          int d2 = 0;
          for (int d = 0; d &lt; k; ++d) {
            d1 += grid[i + d][j + d];
            d2 += grid[i + d][j + k - d - 1];
          }
          s.push_back(d1);
          s.push_back(d2);          
          if (count(begin(s), end(s), s[0]) == s.size()) return k;
        }
    return 1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1895-largest-magic-square/">花花酱 LeetCode 1895. Largest Magic 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-1895-largest-magic-square/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 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 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 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 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 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>
		<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>
	</channel>
</rss>
