|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +
|
| 5 | +
|
| 6 | +-* 149. Max Points on a Line *- |
| 7 | +
|
| 8 | +
|
| 9 | +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. |
| 10 | +
|
| 11 | + |
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +
|
| 16 | +Input: points = [[1,1],[2,2],[3,3]] |
| 17 | +Output: 3 |
| 18 | +Example 2: |
| 19 | +
|
| 20 | +
|
| 21 | +Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] |
| 22 | +Output: 4 |
| 23 | + |
| 24 | +
|
| 25 | +Constraints: |
| 26 | +
|
| 27 | +1 <= points.length <= 300 |
| 28 | +points[i].length == 2 |
| 29 | +-104 <= xi, yi <= 104 |
| 30 | +All the points are unique. |
| 31 | +
|
| 32 | +
|
| 33 | +
|
| 34 | +*/ |
| 35 | +import 'dart:collection'; |
| 36 | +import 'dart:math'; |
| 37 | + |
| 38 | +class Point { |
| 39 | + late int x; |
| 40 | + late int y; |
| 41 | + // Point() { |
| 42 | + // x = 0; |
| 43 | + // y = 0; |
| 44 | + // } |
| 45 | + Point(int a, int b) { |
| 46 | + x = a; |
| 47 | + y = b; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +class A { |
| 52 | + int maxPoints(List<List<int>> points) { |
| 53 | + if (points.length <= 0) return 0; |
| 54 | + if (points.length <= 2) return points.length; |
| 55 | + int result = 0; |
| 56 | + int x = 0; |
| 57 | + int y = 0; |
| 58 | + for (int i = 0; i < points.length; i++) { |
| 59 | + HashMap<double, int> hm = HashMap(); |
| 60 | + int sameX = 1; |
| 61 | + int sameP = 0; |
| 62 | + for (int j = 0; j < points.length; j++) { |
| 63 | + if (j != i) { |
| 64 | + if ((points[j][x] == points[i][x]) && |
| 65 | + (points[j][y] == points[i][y])) { |
| 66 | + sameP++; |
| 67 | + } |
| 68 | + if (points[j][x] == points[i][x]) { |
| 69 | + sameX++; |
| 70 | + continue; |
| 71 | + } |
| 72 | + double k = |
| 73 | + (points[j][y] - points[i][y]) / (points[j][x] - points[i][x]); |
| 74 | + if (hm.containsKey(k)) { |
| 75 | + hm[k] = (hm[k] ?? 0) + 1; |
| 76 | + } else { |
| 77 | + hm[k] = 2; |
| 78 | + } |
| 79 | + result = max(result, (hm[k] ?? 0) + sameP); |
| 80 | + } |
| 81 | + } |
| 82 | + result = max(result, sameX); |
| 83 | + } |
| 84 | + return result; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class B { |
| 89 | + int getGcd(int a, int b) { |
| 90 | + if (a == 0) return b; |
| 91 | + return getGcd(b % a, a); |
| 92 | + } |
| 93 | + |
| 94 | + int maxPoints(List<List<int>> points) { |
| 95 | + int v = 0; |
| 96 | + int res = 0; |
| 97 | + int len = points.length; |
| 98 | + if (len < 3) return len; |
| 99 | + for (int i = 0; i + res < len; i++) { |
| 100 | + HashMap<String, int> m = HashMap(); |
| 101 | + int x1 = points[i][0], y1 = points[i][1], v = 0, maxv = 0, dups = 0; |
| 102 | + int nIdentical = 0; |
| 103 | + // start with i+1, since if any previous point is on the same line, |
| 104 | + // then this was already calculated then that point was a starting point |
| 105 | + for (int j = i + 1; j < len; j++) { |
| 106 | + int x2 = points[j][0], y2 = points[j][1]; |
| 107 | + int dx = x2 - x1, dy = y2 - y1; |
| 108 | + |
| 109 | + if (dx == 0 && dy == 0) { |
| 110 | + dups++; |
| 111 | + } else { |
| 112 | + // we need the slope: dx/dy. but float rounds up the end and produces slightly different results, |
| 113 | + // so instead we keep both dx and dy as the key. |
| 114 | + // to make them identical for the identical slope, use GCD: greatest common divisor |
| 115 | + int gcd = getGcd(dx, dy); |
| 116 | + dy ~/= gcd; |
| 117 | + dx ~/= gcd; |
| 118 | + |
| 119 | + // dx and dy define the slope. |
| 120 | + // we keep the map for the current point i, so the full key is point[i]+slope excludes parallel lines. |
| 121 | + // vertical line: dx==0, horizontal line: dy==0. GCD will set vertical: dx=0, dy=1, horizontal: dx=1, dy=0 |
| 122 | + String key = dx.toString() + '/' + dy.toString(); |
| 123 | + //m[key] != m.entries |
| 124 | + if (m.containsKey(key)) { |
| 125 | + // m[key]++; |
| 126 | + m[key] = (m[key] ?? 0) + 1; |
| 127 | + v = m[key] ?? 0; |
| 128 | + } else { |
| 129 | + m[key] = 1; |
| 130 | + v = 1; |
| 131 | + } |
| 132 | + } |
| 133 | + // duplicates should increase our best result found with point[i] |
| 134 | + if (maxv < v) maxv = v; |
| 135 | + if (res < dups + maxv + 1) res = dups + maxv + 1; |
| 136 | + } |
| 137 | + } |
| 138 | + return res; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +class C { |
| 143 | + int getGcd(int a, int b) { |
| 144 | + if (a == 0) return b; |
| 145 | + return getGcd(b % a, a); |
| 146 | + } |
| 147 | + |
| 148 | + int maxPoints(List<List<int>> points) { |
| 149 | + int res = 0, len = points.length; |
| 150 | + if (len < 3) return len; |
| 151 | + for (int i = 0; i + res < len; i++) { |
| 152 | + Map<String, int> m = {}; |
| 153 | + int x1 = points[i][0], y1 = points[i][1], v = 0, maxV = 0, dups = 0; |
| 154 | + int nIdentical = 0; |
| 155 | + for (int j = i + 1; j < len; j++) { |
| 156 | + int x2 = points[j][0], y2 = points[j][1]; |
| 157 | + int dx = x2 - x1, dy = y2 - y1; |
| 158 | + if (dx == 0 && dy == 0) { |
| 159 | + dups++; |
| 160 | + } else { |
| 161 | + int gcd = getGcd(dx, dy); |
| 162 | + dy ~/= gcd; |
| 163 | + dx ~/= gcd; |
| 164 | + String key = "$dx/$dy"; |
| 165 | + if (m.containsKey(key)) { |
| 166 | + // m[key]++; |
| 167 | + // v = m[key]; |
| 168 | + m[key] = (m[key] ?? 0) + 1; |
| 169 | + v = m[key] ?? 0; |
| 170 | + } else { |
| 171 | + m[key] = 1; |
| 172 | + v = 1; |
| 173 | + } |
| 174 | + } |
| 175 | + if (maxV < v) maxV = v; |
| 176 | + if (res < dups + maxV + 1) res = dups + maxV + 1; |
| 177 | + } |
| 178 | + } |
| 179 | + return res; |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +class D { |
| 184 | + int maxPoints(List<List<int>> pt) { |
| 185 | + int ans = 1; |
| 186 | + int n = pt.length; |
| 187 | + for (int i = 0; i < n - 1; i++) { |
| 188 | + HashMap<double, int> mp = HashMap(); |
| 189 | + for (int j = i + 1; j < n; j++) { |
| 190 | + if (pt[j][1] == pt[i][1]) { |
| 191 | + mp[-90] = (mp[-90] ?? 0) + 1; |
| 192 | + } else { |
| 193 | + double x = (pt[j][0] - pt[i][0]).toDouble() / |
| 194 | + (pt[j][1] - pt[i][1]).toDouble(); |
| 195 | + mp[x] = (mp[x] ?? 0) + 1; |
| 196 | + } |
| 197 | + } |
| 198 | + int temp = 0; |
| 199 | + for (MapEntry<double, int> it in mp.entries) { |
| 200 | + temp = max(temp, it.value + 1); |
| 201 | + } |
| 202 | + ans = max(temp, ans); |
| 203 | + } |
| 204 | + return ans; |
| 205 | + } |
| 206 | +} |
0 commit comments