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

# 1 <= points.length <= 300
# All the points are unique

# Option 1. Brutal force
# use two points to define a line, check if other points are on the same line
# O(N**3)

# Option 2. Caching
# use two points to define a line, and cache slope with dict {slope: (p1, p2)}
# if two lines have the same slope and connected point, then +1 in the path, this can be resolved with DFS

# Start with Option 2

class Solution:
    def count_max(self, points_list: List[Tuple[Tuple[int, int], Tuple[int, int]]]) -> int:
        if len(points_list) == 1:
            return 2

        # build a graph
        point_map: Dict[Tuple[int, int], int] = {}
        curr_index = 0
        for points in points_list:
            for point in points:
                if point not in point_map:
                    point_map[point] = curr_index
                    curr_index += 1
        # print(point_map)

        n = len(point_map)
        graph: List[List[int]] = [[0] * n for _ in range(n)]

        for points in points_list:
            p1 = points[0]
            p2 = points[1]
            index1 = point_map[p1]
            index2 = point_map[p2]
            graph[index1][index2] = 1
            graph[index2][index1] = 1
        # print(graph)

        # dfs
        visited: Set[int] = set()
        max_points = 0
        for i in range(n):
            if i in visited:
                continue

            curr_points = 0
            stack: List[int] = []
            stack.append(i)
            while len(stack) > 0:
                curr_index = stack.pop()
                if curr_index not in visited:
                    visited.add(curr_index)
                    curr_points += 1

                for j in range(n):
                    if graph[curr_index][j] == 1:
                        if j not in visited:
                            stack.append(j)

            max_points = max(max_points, curr_points)

        return max_points

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

        slope_to_points: Dict[float, List[Tuple[Tuple[int, int], Tuple[int, int]]]] = {}

        for i in range(len(points)):
            for j in range(i + 1, len(points)):
                p1 = points[i]
                p2 = points[j]
                if p2[0] - p1[0] == 0:
                    slope = float(sys.maxsize)
                else:
                    slope = (p2[1] - p1[1]) / (p2[0] - p1[0])
                if slope in slope_to_points:
                    slope_to_points[slope].append(((p1[0], p1[1]), (p2[0], p2[1])))
                else:
                    slope_to_points[slope] = [((p1[0], p1[1]), (p2[0], p2[1]))]
        # print(slope_to_points)

        max_points = 0
        for points_list in slope_to_points.values():
            curr_points = self.count_max(points_list)
            max_points = max(max_points, curr_points)

        return max_points
29939