-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxPoints.java
70 lines (55 loc) · 1.13 KB
/
MaxPoints.java
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
import java.util.*;
public class MaxPoints {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int maxPoints(Point[] points) {
if(points.length <= 2)
{
return points.length;
}
int max = 2;
for(int i = 0; i<points.length; i++)
{
//Choose different start point
Map<Double, Integer> map = new HashMap<Double, Integer>();
int offset = 0;
for(int j = 0; j<points.length;j++)
{
if(j == i)
{
continue;
}
if(points[j].x == points[i].x && points[j].y == points[i].y)
{
offset++;
continue;
}
double slope = (points[j].y - points[i].y)*1.0/(points[j].x - points[i].x);
if(!map.containsKey(slope))
{
map.put(slope, 2);
}
else
{
map.put(slope, map.get(slope)+1);
}
}
for(Double d: map.keySet())
{
max = Math.max(max, offset + map.get(d));
}
max = Math.max(max, offset+1);
}
return max;
}
class Point {
int x;
int y;
Point() { x = 0; y = 0; }
Point(int a, int b) { x = a; y = b; }
}
}