|
| 1 | +--- |
| 2 | +id: max-points-on-a-line.md |
| 3 | +title: Max Points on a Line |
| 4 | +sidebar_label: 0149- Max Points on a Line |
| 5 | +tags: |
| 6 | + - Leetcode |
| 7 | +description: "This is a solution to the Max Point on a Line on LeetCode." |
| 8 | +--- |
| 9 | + |
| 10 | +### Problem Satement |
| 11 | + |
| 12 | +Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line. |
| 13 | + |
| 14 | +### Examples |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +``` |
| 19 | +Input: points = [[1,1],[2,2],[3,3]] |
| 20 | +Output: 3 |
| 21 | +``` |
| 22 | + |
| 23 | +**Example 2:** |
| 24 | + |
| 25 | +``` |
| 26 | +Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] |
| 27 | +Output: 4 |
| 28 | +``` |
| 29 | + |
| 30 | +### Constraints: |
| 31 | + |
| 32 | +- $1 <= points.length <= 300$ |
| 33 | +- $points[i].length == 2$ |
| 34 | +- $-10^4 <= xi, yi <= 10^4$ |
| 35 | +- All the points are unique. |
| 36 | + |
| 37 | +### Algorithm Explanation |
| 38 | + |
| 39 | +1. **Input Handling**: |
| 40 | + |
| 41 | + - If there are 2 or fewer points, all points will always lie on the same line, so we can directly return the number of points. |
| 42 | + |
| 43 | +2. **Initialization**: |
| 44 | + |
| 45 | + - Initialize `maxi` to 2, since at least two points can define a line. |
| 46 | + |
| 47 | +3. **Iterate through each pair of points**: |
| 48 | + |
| 49 | + - Use a nested loop to select pairs of points `(i, j)` where `i < j`. |
| 50 | + |
| 51 | +4. **Count points on the line**: |
| 52 | + |
| 53 | + - For each pair `(i, j)`, initialize a count of 2 (since the pair itself constitutes two points on the line). |
| 54 | + - Check every other point `k` to see if it lies on the line defined by `(i, j)`. |
| 55 | + - To determine if three points are collinear, use the slope comparison: |
| 56 | + `(y2 - y1) \times (x1 - x3) = (y1 - y3) \times (x2 - x1)` |
| 57 | + This equation avoids division and thus floating-point inaccuracies. |
| 58 | + |
| 59 | +5. **Update Maximum Count**: |
| 60 | + |
| 61 | + - Update `maxi` if the current line defined by points `(i, j)` has more points than the previously recorded maximum. |
| 62 | + |
| 63 | +6. **Return Result**: |
| 64 | + - After checking all pairs, return the maximum number of collinear points found. |
| 65 | + |
| 66 | +### Code Implementation |
| 67 | + |
| 68 | +#### C++ Implementation |
| 69 | + |
| 70 | +```cpp |
| 71 | +class Solution { |
| 72 | +public: |
| 73 | + int maxPoints(vector<vector<int>>& points) { |
| 74 | + int n = points.size(); |
| 75 | + |
| 76 | + if(n <= 2) |
| 77 | + return n; |
| 78 | + |
| 79 | + int maxi = 2; |
| 80 | + for(int i = 0; i < n; i++) { |
| 81 | + for(int j = i + 1; j < n; j++) { |
| 82 | + int count = 2; |
| 83 | + for(int k = 0; k < n; k++) { |
| 84 | + if(k != i && k != j) { |
| 85 | + if((points[j][1] - points[i][1]) * (points[i][0] - points[k][0]) == |
| 86 | + (points[i][1] - points[k][1]) * (points[j][0] - points[i][0])) { |
| 87 | + count++; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + maxi = max(maxi, count); |
| 92 | + } |
| 93 | + } |
| 94 | + return maxi; |
| 95 | + } |
| 96 | +}; |
| 97 | +``` |
| 98 | + |
| 99 | +#### Python Implementation |
| 100 | + |
| 101 | +```python |
| 102 | +class Solution: |
| 103 | + def maxPoints(self, points: List[List[int]]) -> int: |
| 104 | + n = len(points) |
| 105 | + |
| 106 | + if n <= 2: |
| 107 | + return n |
| 108 | + |
| 109 | + maxi = 2 |
| 110 | + for i in range(n): |
| 111 | + for j in range(i + 1, n): |
| 112 | + count = 2 |
| 113 | + for k in range(n): |
| 114 | + if k != i and k != j: |
| 115 | + if (points[j][1] - points[i][1]) * (points[i][0] - points[k][0]) == \ |
| 116 | + (points[i][1] - points[k][1]) * (points[j][0] - points[i][0]): |
| 117 | + count += 1 |
| 118 | + maxi = max(maxi, count) |
| 119 | + return maxi |
| 120 | +``` |
| 121 | + |
| 122 | +#### Java Implementation |
| 123 | + |
| 124 | +```java |
| 125 | +class Solution { |
| 126 | + public int maxPoints(int[][] points) { |
| 127 | + int n = points.length; |
| 128 | + |
| 129 | + if (n <= 2) |
| 130 | + return n; |
| 131 | + |
| 132 | + int maxi = 2; |
| 133 | + for (int i = 0; i < n; i++) { |
| 134 | + for (int j = i + 1; j < n; j++) { |
| 135 | + int count = 2; |
| 136 | + for (int k = 0; k < n; k++) { |
| 137 | + if (k != i && k != j) { |
| 138 | + if ((points[j][1] - points[i][1]) * (points[i][0] - points[k][0]) == |
| 139 | + (points[i][1] - points[k][1]) * (points[j][0] - points[i][0])) { |
| 140 | + count++; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + maxi = Math.max(maxi, count); |
| 145 | + } |
| 146 | + } |
| 147 | + return maxi; |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +#### JavaScript Implementation |
| 153 | + |
| 154 | +```javascript |
| 155 | +var maxPoints = function (points) { |
| 156 | + let n = points.length; |
| 157 | + |
| 158 | + if (n <= 2) return n; |
| 159 | + |
| 160 | + let maxi = 2; |
| 161 | + for (let i = 0; i < n; i++) { |
| 162 | + for (let j = i + 1; j < n; j++) { |
| 163 | + let count = 2; |
| 164 | + for (let k = 0; k < n; k++) { |
| 165 | + if (k !== i && k !== j) { |
| 166 | + if ( |
| 167 | + (points[j][1] - points[i][1]) * (points[i][0] - points[k][0]) === |
| 168 | + (points[i][1] - points[k][1]) * (points[j][0] - points[i][0]) |
| 169 | + ) { |
| 170 | + count++; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + maxi = Math.max(maxi, count); |
| 175 | + } |
| 176 | + } |
| 177 | + return maxi; |
| 178 | +}; |
| 179 | +``` |
0 commit comments