1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| import java.math.BigInteger;
class Solution { int[][] points; int n; HashMap<Pair<Integer, Integer>, Integer> lines = new HashMap<Pair<Integer, Integer>, Integer>(); int horizontal_lines;
private Pair<Integer, Integer> slope_coprime(int x1, int y1, int x2, int y2) { int deltaX = x1 - x2, deltaY = y1 - y2; if (deltaX == 0) return new Pair<>(0, 0); else if (deltaY == 0) return new Pair<>(Integer.MAX_VALUE, Integer.MAX_VALUE); else if (deltaX < 0) { deltaX = -deltaX; deltaY = -deltaY; } Integer gcd = BigInteger.valueOf(deltaX).gcd(BigInteger.valueOf(deltaY)).intValue();
return new Pair<Integer, Integer>(deltaX / gcd, deltaY / gcd); }
public Pair<Integer, Integer> add_line(int i, int j, int count, int duplicates) {
int x1 = points[i][0]; int y1 = points[i][1]; int x2 = points[j][0]; int y2 = points[j][1]; if ((x1 == x2) && (y1 == y2)) duplicates++; else if (y1 == y2) { horizontal_lines += 1; count = Math.max(horizontal_lines, count); } else { Pair<Integer, Integer> slope = this.slope_coprime(x1, y1, x2, y2); lines.put(slope, lines.getOrDefault(slope, 1) + 1); count = Math.max(lines.get(slope), count); } return new Pair(count, duplicates); }
public int max_points_on_a_line_containing_point_i(int i) {
lines.clear(); horizontal_lines = 1; int count = 1; int duplicates = 0;
for (int j = i + 1; j < n; j++) { Pair<Integer, Integer> p = add_line(i, j, count, duplicates); count = p.getKey(); duplicates = p.getValue(); } return count + duplicates; }
public int maxPoints(int[][] points) {
this.points = points; n = points.length; if (n < 3) return n;
int max_count = 1; for (int i = 0; i < n - 1; i++) max_count = Math.max(max_points_on_a_line_containing_point_i(i), max_count); return max_count; } }
|