We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 828d8e4 + 9a14fbe commit 95403a7Copy full SHA for 95403a7
java/0149-max-points-on-a-line.java
@@ -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