<?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>eary Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/eary/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/eary/</link>
	<description></description>
	<lastBuildDate>Thu, 23 Dec 2021 21:21:33 +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>eary Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/eary/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
	</channel>
</rss>
