-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMostPointsOnALine.java
More file actions
162 lines (139 loc) · 6.4 KB
/
MostPointsOnALine.java
File metadata and controls
162 lines (139 loc) · 6.4 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Given an array of 2D coordinates of points (all the coordinates are integers),
find the largest number of points that can be crossed by a single line in 2D space.
Assumptions
The given array is not null, and it has at least 2 points
Examples
<0, 0>, <1, 1>, <2, 3>, <3, 3>, the maximum number of points on a line is 3(<0, 0>, <1, 1>, <3, 3> are on the same line)
*/
import java.awt.Point;
import java.util.*;
import java.util.List;
public class MostPointsOnALine {
//Use below wrapper to submit to leetCode 149. Max Points on a Line (https://leetcode.com/problems/max-points-on-a-line/)
public int maxPoints(int[][] points) {
Point[] points2 = new Point[points.length];
for (int i = 0; i < points.length; i++) {
points2[i] = new Point(points[i][0], points[i][1]);
}
return most(points2);
}
// method 1 start, simple and can handle duplicates
public int most(Point[] points) { // TC: O(n^2), SC: O(n)
int max = 0;
for (Point point1 : points) {
int same = 0, sameX = 0, slopeMax = 0;
Map<Double, Integer> map = new HashMap<>();
for (Point point2 : points) {
if (point1.x == point2.x && point1.y == point2.y) same++;
else if (point1.x == point2.x) sameX++;
else {
double slope = (point2.y - point1.y + .0) / (point2.x - point1.x);
int count = map.getOrDefault(slope, 0) + 1;
map.put(slope, count);
slopeMax = Math.max(slopeMax, count);
}
}
max = Math.max(max, Math.max(sameX, slopeMax) + same);
}
return max;
}
// method 1 ends here
// Method 2 using slope(a) and intercept(b) pair to denote a line: y = ax + b;
static class Line {
double slope;
double intercept;
Line(double slope, double intercept) {
this.slope = slope;
this.intercept = intercept;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Line line = (Line) o;
return Double.compare(line.slope, slope) == 0 && Double.compare(line.intercept, intercept) == 0;
}
@Override
public int hashCode() {
return Objects.hash(slope, intercept);
}
}
public int most2(Point[] points) { // TC: O(n^2), SC: O(n)
int max = 0;
Map<Line, Set<Point>> m1 = new HashMap<>();
Map<Integer, Set<Point>> m2 = new HashMap<>();
for (Point point1 : points) {
for (Point point2 : points) {
if (point1.x == point2.x) {
Set<Point> set = m2.getOrDefault(point1.x, new HashSet<>());
set.add(point1);
set.add(point2);
m2.put(point1.x, set);
max = Math.max(max, set.size());
} else {
double slope = (point2.y - point1.y + .0) / (point2.x - point1.x);
double intercept = point1.y - slope * point1.x;
Line p = new Line(slope, intercept);
Set<Point> set = m1.getOrDefault(p, new HashSet<>());
set.add(point1);
set.add(point2);
m1.put(p, set);
max = Math.max(max, set.size());
}
}
}
return max;
}
// method 2 ends here
// method 3 just for fun, only modify to method 2 is using List for storing pair, as List has equals & hashCode implemented
public int most3(Point[] points) { // TC: O(n^2), SC: O(n)
int max = 0;
Map<List<Double>, Set<Point>> m1 = new HashMap<>();
Map<Integer, Set<Point>> m2 = new HashMap<>();
for (Point point1 : points) {
for (Point point2 : points) {
if (point1.x == point2.x) {
Set<Point> set = m2.getOrDefault(point1.x, new HashSet<>());
set.add(point1);
set.add(point2);
m2.put(point1.x, set);
max = Math.max(max, set.size());
} else {
double slope = (point2.y - point1.y + .0) / (point2.x - point1.x);
double intercept = point1.y - slope * point1.x;
List<Double> pair = Arrays.asList(slope, intercept);
Set<Point> set = m1.getOrDefault(pair, new HashSet<>());
set.add(point1);
set.add(point2);
m1.put(pair, set);
max = Math.max(max, set.size());
}
}
}
return max;
}
// method 3 ends here
public static void main(String[] args) {
MostPointsOnALine mpa = new MostPointsOnALine();
Point[] points = {new Point(0,0), new Point(1,1), new Point(2,3), new Point(3,3), };
System.out.println(mpa.most(points)); // 3
Point[] points2 = {new Point(1,1), new Point(5,1), new Point(2,2), new Point(3,3), new Point(4,4), new Point(6,2), new Point(7,3), };
System.out.println(mpa.most(points2)); // 4
Point[] points3 = {new Point(1,1), new Point(2,2), new Point(3,3), };
System.out.println(mpa.most2(points3)); // 3
System.out.println(mpa.most3(points2)); // 4
// to demo List have equals and hashCode implemented
List<Integer> l1 = Arrays.asList(1, 2, 3);
List<Integer> l2 = Arrays.asList(1, 2, 3);
System.out.printf("l1 == l2 : %s\n", l1 == l2); // false
System.out.printf("l1.equals(l2) : %s\n", l1.equals(l2)); // true
System.out.printf("l1.hashCode() == l2.hashCode() : %s\n", l1.hashCode() == l2.hashCode()); // true
// test our hashCode and equals implementation for Pair
Line line1 = new Line(1.9087654321, 2.1023456789);
Line line2 = new Line(1.9087654321, 2.1023456789);
System.out.printf("pair1 == pair2 : %s\n", line1 == line2); // false
System.out.printf("pair1.equals(pair2) : %s\n", line1.equals(line2)); // true
System.out.printf("pair1.hashCode() == pair2.hashCode() : %s\n", line1.hashCode() == line2.hashCode()); // true
}
}