The following is my submission for LeetCode's Max Points on a Line:
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. Example 1:
Input: points = [[1,1],[2,2],[3,3]] Output: 3
I looked at other answers in the discussion forum and I'm not at all sure why my code runs so terribly in comparison to other answers, as it seems the general approach is the correct one(?).
public static int maxPoints(int[][] points) {
Map<String, Set<int[]>> lines = new HashMap<>();
for (int i = 0; i < points.length; i++) {
int x1 = points[i][0];
int y1 = points[i][1];
for (int j = 0; j < points.length; j++) {
if (i == j) continue;
int x2 = points[j][0];
int y2 = points[j][1];
double m = x2 - x1 == 0 ? Integer.MAX_VALUE : ((double) (y2 - y1)) / (x2 - x1);
double c = y2 - m * x2;
String eq = m + "x+" + c;
Set<int[]> pointsOnLine = lines.getOrDefault(eq, new HashSet<>());
pointsOnLine.add(points[i]);
pointsOnLine.add(points[j]);
lines.put(eq, pointsOnLine);
}
}
int max = 1;
for (Set<int[]> value : lines.values()) {
max = Math.max(value.size(), max);
}
return max;
}
I would appreciate feedback since I'm assuming I am misunderstanding/not realising how bad something that seems fairly normal to me is written.

