|
| 1 | +""" |
| 2 | +Maximum Collinear Points |
| 3 | +Given an array of distinct points in 2D space, find the maximum number of points that lie on the same line. |
| 4 | +
|
| 5 | +Example #1: |
| 6 | +Input: [[0, 1], [1, 0], [2, 0]] |
| 7 | +
|
| 8 | + - |
| 9 | + | |
| 10 | + - |
| 11 | + | |
| 12 | + 🔵 |
| 13 | + | |
| 14 | +|--|--|--|--🔵--🔵--| |
| 15 | + | |
| 16 | + - |
| 17 | + | |
| 18 | + - |
| 19 | + | |
| 20 | + - |
| 21 | +
|
| 22 | +Output: 2 |
| 23 | +Explanation: Choose any two points; they lie on the same line. Since the three points aren’t collinear, it’s clear that we can’t do any better. |
| 24 | +
|
| 25 | +Example #2: |
| 26 | +Input: [[0, 1], [1, 0], [2, 0], [10, 0]] |
| 27 | +
|
| 28 | + - |
| 29 | + | |
| 30 | + - |
| 31 | + | |
| 32 | + 🔵 |
| 33 | + | |
| 34 | +|--|--|--|--🔵--🔵--|--|--|--|--|--|--|--🔵--| |
| 35 | + | |
| 36 | + - |
| 37 | + | |
| 38 | + - |
| 39 | + | |
| 40 | + - |
| 41 | +
|
| 42 | +Output: 3 |
| 43 | +Explanation: Choose the points (1, 0), (2, 0), and (10, 0). These three points are collinear, and this is an optimal solution. |
| 44 | +""" |
| 45 | + |
| 46 | + |
| 47 | +class Solution: |
| 48 | + def maximumCollinearPoints(self, points): |
| 49 | + """ |
| 50 | + Interface |
| 51 | + ---- |
| 52 | + :type points: list of list of int |
| 53 | + :rtype: int |
| 54 | +
|
| 55 | + Approach |
| 56 | + ---- |
| 57 | + Slope Approach |
| 58 | + 1. A brute force solution is to simply traverse all possible pairs of points and count the number of points that are on the |
| 59 | + line determined by the pair of points we are iterating over. |
| 60 | +
|
| 61 | + 2. This gives a cubic-time solution. However, we can reduce our time complexity by utilizing hashing. |
| 62 | +
|
| 63 | + 3. Instead of traversing all possible pairs of points, we traverse all possible lines. |
| 64 | +
|
| 65 | + 4. For each point in our array, we can compute the slope of the line generated by this point and every other point in our array. |
| 66 | +
|
| 67 | + 5.Any two points that have the same slope relative to the point that we are iterating over will lie on the same line. |
| 68 | +
|
| 69 | + 6. Thus, we can keep a count of the number of points whose slope relative to the point that we are iterating over is equal to some value, |
| 70 | + and we can take the maximum over all such counters to determine our final answer. |
| 71 | +
|
| 72 | + 7. To keep such a counter, one approach is to a hashtable that maps a floating-point number (representing the slope of each line) to an integer (representing |
| 73 | + a count of the number of points whose slope relative to the current point equals that value). |
| 74 | +
|
| 75 | + 8. However, it is usually not a good idea to use floating-point numbers as keys when hashing due to potential precision issues. |
| 76 | + Thus, we instead use strings of the form "dy/dx" (where "dy" and "dx" are the change in y and x-values respectively) as our keys, |
| 77 | + and these keys map to the number of times that this slope appears relative to the point being iterated over. |
| 78 | +
|
| 79 | + 9. We also ensure that our fraction is in reduced form by dividing both "dy" and "dx" by their greatest common divisor so that we do |
| 80 | + not end up keeping multiple counters for the same slope value. |
| 81 | +
|
| 82 | + Complexity |
| 83 | + ---- |
| 84 | + Time : |
| 85 | + Space : O(N) |
| 86 | + """ |
| 87 | + |
| 88 | + # Shortcut: If we have two or less points, we can take all of them. |
| 89 | + if len(points) <= 2: |
| 90 | + return len(points) |
| 91 | + |
| 92 | + answer = 0 |
| 93 | + for i in range(0, len(points)): |
| 94 | + |
| 95 | + # Hash Table for storing the slopes |
| 96 | + # In the form of a string |
| 97 | + slope_counter = {} |
| 98 | + |
| 99 | + # Calulate the slope for each pair |
| 100 | + for j in range(i + 1, len(points)): |
| 101 | + dy = points[j][1] - points[i][1] |
| 102 | + dx = points[j][0] - points[i][0] |
| 103 | + |
| 104 | + # Gcd is calculated for reducing fractions |
| 105 | + gcd = self.get_gcd(dy, dx) |
| 106 | + |
| 107 | + # Make sure our fractions are fully reduced. |
| 108 | + dy //= gcd |
| 109 | + dx //= gcd |
| 110 | + |
| 111 | + # Convert slopes to strings; avoid dealing with floating-point precision. |
| 112 | + signature = f'{dy}/{dx}' |
| 113 | + if signature in slope_counter: |
| 114 | + slope_counter[signature] += 1 |
| 115 | + else: |
| 116 | + slope_counter[signature] = 1 |
| 117 | + # Update the maximum answer |
| 118 | + for key, pointsCountForSlope in slope_counter.items(): |
| 119 | + # Add 1 to include the point we iterated off of. |
| 120 | + answer = max(answer, pointsCountForSlope + 1) |
| 121 | + |
| 122 | + return answer |
| 123 | + |
| 124 | + # Helper Function to calculate GCD |
| 125 | + # gcd(0,n) = gcd(n,0) = n |
| 126 | + def get_gcd(self, a, b): |
| 127 | + if a == 0: |
| 128 | + return b |
| 129 | + if b == 0: |
| 130 | + return a |
| 131 | + r = a % b |
| 132 | + |
| 133 | + return self.get_gcd(b, r) if r != 0 else b |
| 134 | + |
| 135 | + |
| 136 | +i = [[0, 1], [1, 0], [2, 0], [10, 0]] |
| 137 | +print(Solution().maximumCollinearPoints(i)) |
0 commit comments