<p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= points.length &lt;= 300</code></li>
	<li><code>points[i].length == 2</code></li>
	<li><code>-10<sup>4</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
	<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>

import collections
import itertools
import math

class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:

        if len(points) == 1:
            return 1
    
        gcd_cache = {}
        line_descriptors = collections.Counter()
        for p1, p2 in itertools.combinations(points, 2):
            line_descriptors[self.getLineDescriptor(p1, p2, gcd_cache)] += 1
        
        sum_of_pairs = line_descriptors.most_common(1)[0][1]

        ans = math.ceil(math.sqrt(2*sum_of_pairs)) 

        return ans

    def getLineDescriptor(self, p1: Tuple[int], p2: Tuple[int], gcd_cache) -> Tuple[int]:
        """
        Returns slope_num, slope_denom, intercept
        """
        x1, y1 = p1
        x2, y2 = p2

        m_num = y2 - y1
        m_denom = x2 - x1

        if m_num == 0:
            return 0, 0, y1
        
        if m_denom == 0:
            return math.inf, math.inf, x1

        if m_denom < 0:
            m_num *= -1
            m_denom *= -1

        m_num_abs = abs(m_num)
        m_denom_abs = abs(m_denom)

        gcd_cache_key = (m_num_abs, m_denom_abs)
        if gcd_cache_key in gcd_cache:
            gcd = gcd_cache[gcd_cache_key]
        else:
            gcd = math.gcd(m_num_abs, m_denom_abs)
            gcd_cache[gcd_cache_key] = gcd

        m_num /= gcd
        m_denom /= gcd

        return m_num, m_denom, y1*m_denom - m_num*x1


30556