<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 __init__(self):
        self.line_dict = dict()

    def maxPoints(self, points: List[List[int]]) -> int:
    
        if len(points) < 3:
            return len(points)

        max_qty = 2
        for i in range(len(points) - 1):
            self.line_dict[i] = set()
            for j in range(i + 1, len(points)):
                self.line_dict[i].add(j)
                
        # print(len(self.line_dict), self.line_dict)
        
        # traverse
        for start, ends in self.line_dict.items():
            # print(str_p, ends)
            str_p = points[start]
            end_point_list = list(ends)
            # print(str_p, end_point_list)
            # 以str_p為起始點，逐個點做為end_p檢驗連線上有沒有其他點
            for i in range(len(end_point_list)):
                curr_line = set()
                curr_line.add(start)
                if end_point_list[i] not in self.line_dict[start]:
                    continue
                end_p = points[end_point_list[i]]
                curr_line.add(end_point_list[i])
                # print(f"current line: from {str_p} to {end_p}")
                var_se_x = end_p[0] - str_p[0]
                var_se_y = end_p[1] - str_p[1]
                for j in range(i + 1, len(end_point_list)):
                    if end_point_list[j] not in self.line_dict[start]:
                        continue
                    ext_p = points[end_point_list[j]]
                    # print(f"斜率: {var_se_x}, {var_se_y} - 測試點: {ext_p}")
                    var_eo_x = ext_p[0] - end_p[0]
                    var_eo_y = ext_p[1] - end_p[1]
                    if var_eo_x * var_se_y == var_eo_y * var_se_x:
                        # print(f"3點連成一線: {str_p},{end_p},{ext_p}")
                        self.line_dict[start].remove(end_point_list[j])
                        # self.line_dict[end_point_list[i]].remove(end_point_list[j])
                        curr_line.add(end_point_list[j])
                    # print()
                    max_qty = max(max_qty, len(curr_line))
                # print(self.line_dict)
            
        
        return max_qty
19133