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

# Hard problem. Max Points on a Line.
# Given an array of points where points[i] = [x, y]
# Return the maximum number of points on the same straight line

# Ok, let's remember from algebra,
# the equation of a line is:
# y = m * x + b
# And given two points, we can find a line between them

# We notice that a point can either be on the maximum line
# or not on the maximum line

# the maximum line is the line that has the most points on it

# The minimum answer is going to be 1 if points.length == 1
# and 2 is points.length == 2
# If the max len is 3 and points.length is 3, then all points
# are on the same line

# It might take some work to code up the function verifying
# If a point is on line A, but it won't be that hard, I
# was good at algebra

# We have up to 300 points
# 300^2 is a reasonable solution
# 300^3 == 300 * 90000 == 27000000
# which is even doable
# One point will have up to 300 lines
# Why don't we enumerate every line, check whether other points are on that line
# and count for that line?
# If there an easy way to tell if a point is on the line?
# All we have of a point is its x and y position
# Maybe if we plug x and y into the equation of the line,
# and we get an equality, the point is on our line
# I think I know what to do
# Create every line from each point to each other point
# check all points if they are on that line
# bam

# Maybe we could use a set of lines so that we don't
# have to recheck
class Solution:
    def maxPoints(self, points: list[list[int]]) -> int:
        if len(points) <= 2: return len(points)
        def defval():
            return []
        line_set = defaultdict(defval)
    
        for i in range(len(points)):
            x1, y1 = points[i]
            #print("starting: {}, {}".format(x1, y1))
            for j in range(i + 1, len(points)):
                x2, y2 = points[j]
                
                if x1 == x2:
                    m = float('inf')
                    b = x1
                else:
                    m = (y2 - y1) / (x2 - x1)
                    b = y1 - (m * x1)
                
                #print(x2, y2, m, b)
                intersects = line_set[(m,b)]
                if (x1, y1) not in intersects:
                    intersects.append((x1, y1))
                if (x2, y2) not in intersects:
                    intersects.append((x2, y2))
                
        #print("lines:")
        max_points = 0
        for line in line_set:
            max_points = max(max_points, len(line_set[line]))
            #print(line, line_set[line])
        return max_points
35496