<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>

class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:
        """
        How to figure this out:
        
        Brute force way:

        between every pair, there is a line. you can calculate all of these lines
        and check if any of the other ones fit on the line
        """
        tol =  0.000001
        n = len(points)
        max_points = 1

        seen = set()
        for i in range(0, n):
            for x in range(0, n):
                if x == i or (min(i, x), max(i, x)) in seen:
                    pass
                else:
                    seen.add((min(i, x), max(i, x)))
                    p1 = points[i]
                    p2 = points[x]
                    vertical = None
                    if p2[0] == p1[0]:
                        slope = float('inf')
                        intercept = None
                        vertical = p1[0] #stores the x value 
                    else:
                        slope = (p2[1] - p1[1]) / (p2[0] - p1[0])
                        intercept = - slope * p1[0] + p1[1]  # y = mx + b (solve for b so b = -mx + y)
                    num_points = 2
                    for j in range(0, n):
                        if j not in {x, i}:
                            if vertical is not None:
                                if points[j][0] == vertical:
                                    num_points += 1
                                    seen.add((min(i,j), max(i, j)))
                                    seen.add((min(x,j), max(x, j)))
                            else:
                                if abs(points[j][0] * slope + intercept - points[j][1]) <= tol:
                                    num_points += 1
                                    seen.add((min(i,j), max(i, j)))
                                    seen.add((min(x,j), max(x, j)))
                    
                    max_points = max(num_points, max_points)

                    if max_points > n // 2:
                        return max_points

        return max_points


22220