Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95403a7

Browse files
Merge pull request neetcode-gh#3449 from Tetsuya3850/patch-3
Create 0149-max-points-on-a-line.java
2 parents 828d8e4 + 9a14fbe commit 95403a7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

java/0149-max-points-on-a-line.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int maxPoints(int[][] points) {
3+
int res = 1;
4+
for (int i = 0; i < points.length; i++) {
5+
int[] p1 = points[i];
6+
Map<Double, Integer> counter = new HashMap<>();
7+
for (int j = i + 1; j < points.length; j++) {
8+
int[] p2 = points[j];
9+
double slope;
10+
if (p1[0] == p2[0]) {
11+
slope = Double.MAX_VALUE;
12+
} else if (p1[1] == p2[1]) {
13+
slope = 0;
14+
} else {
15+
slope = (double) (p2[1] - p1[1]) / (double) (p2[0] - p1[0]);
16+
}
17+
counter.put(slope, counter.getOrDefault(slope, 0) + 1);
18+
res = Math.max(res, counter.get(slope) + 1);
19+
}
20+
}
21+
return res;
22+
}
23+
}

0 commit comments

Comments
 (0)