Skip to content

Commit 1837ee9

Browse files
149. Max Points on a Line (java)
1 parent e3671b1 commit 1837ee9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int maxPoints(Point[] points) {
3+
if( points.length <= 2 ) return points.length;
4+
int max = 2 ;
5+
for( int i = 0 ; i < points.length ; i++ ){
6+
int samePosition = 0;
7+
int sameSlope = 1;
8+
for( int j = i + 1 ; j < points.length ; j++ ){
9+
long x1 = points[j].x - points[i].x;
10+
long y1 = points[j].y - points[i].y;
11+
if( x1 == 0 && y1 == 0 ){
12+
samePosition++;
13+
} else {
14+
sameSlope++;
15+
for(int k = j + 1 ; k < points.length ; k++ ){
16+
long x2 = points[k].x - points[i].x;
17+
long y2 = points[k].y - points[i].y;
18+
if ( x1 * y2 == x2 * y1 ) sameSlope++;
19+
}
20+
}
21+
if(max < (samePosition + sameSlope)) max = samePosition + sameSlope;
22+
sameSlope = 1;
23+
}
24+
}
25+
return max;
26+
}
27+
}

0 commit comments

Comments
 (0)