<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 operator
class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:
        
        def slope(id1,id2):
            if id2[0]-id1[0] != 0:
                return (id2[1]-id1[1])/(id2[0]-id1[0])
            else:
                return float('inf')

        def ontheline(id1,id2,id3):
            if (id1[0]-id2[0])*(id1[1]-id3[1]) == (id1[0]-id3[0])*(id1[1]-id2[1]):
                return True
            else:
                return False

        slopemap = {}
        slope_lines = []
        for iid1 in range(len(points)-1):
            for iid2 in range(iid1+1,len(points),1):
                id1 = tuple(points[iid1])
                id2 = tuple(points[iid2])
                s12 = slope(id1,id2)
                if s12 not in slopemap:
                    slopemap[s12] = len(slope_lines)
                    slope_lines.append([{id1,id2},])
                else:
                    flag = False
                    j = slopemap[s12]
                    lines = slope_lines[j]
                    for l in range(len(lines)):
                        line = lines[l]
                        if len(line & {id1,id2}) == 2:
                            flag = True
                            break
                        elif len(line & {id1,id2}) == 1:
                            slope_lines[j][l] = line | {id1,id2}
                            # print(lines, id1, id2)
                            flag = True
                            break
                        else:
                            id3 = next(iter(line))
                            if ontheline(id1,id2,id3):
                                slope_lines[j][l]= line | {id1,id2} 
                                # print(lines, id1, id2, id3)
                                flag = True
                                break
                    if flag is False:
                        slope_lines[j].append({id1,id2})
                            # print(lines, id1, id2)
        maxcnt = 1
        for lines in slope_lines:
            for line in lines:
                maxcnt = max(len(line),maxcnt)
        return maxcnt   


37349