Skip to content

Commit 078e283

Browse files
refactor 149
1 parent 8e1009b commit 078e283

File tree

1 file changed

+33
-65
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+33
-65
lines changed

src/main/java/com/fishercoder/solutions/_149.java

+33-65
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,42 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6-
/**
7-
* 149. Max Points on a Line
8-
*
9-
* Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
10-
*
11-
* Example 1:
12-
* Input: [[1,1],[2,2],[3,3]]
13-
* Output: 3
14-
* Explanation:
15-
* ^
16-
* |
17-
* | o
18-
* | o
19-
* | o
20-
* +------------->
21-
* 0 1 2 3 4
22-
*
23-
* Example 2:
24-
* Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
25-
* Output: 4
26-
* Explanation:
27-
* ^
28-
* |
29-
* | o
30-
* | o o
31-
* | o
32-
* | o o
33-
* +------------------->
34-
* 0 1 2 3 4 5 6
35-
*
36-
* NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
37-
*/
386
public class _149 {
397

40-
/**
41-
* credits: https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding
42-
*/
43-
public static class Solution1 {
44-
public int maxPoints(int[][] points) {
45-
if (points.length < 3) {
46-
return points.length;
47-
}
48-
int max = 0;
49-
Map<Long, Integer> map = new HashMap<>();
50-
for (int i = 0; i < points.length; i++) {
51-
int dup = 1;
52-
map.clear();
53-
for (int j = i + 1; j < points.length; j++) {
54-
int dx = points[j][0] - points[i][0];
55-
int dy = points[j][1] - points[i][1];
56-
if (dx == 0 && dy == 0) {
57-
dup++;
58-
} else {
59-
int gcd = getGcd(dx, dy);
60-
long slope = ((long) (dy / gcd) << 32) + (dx / gcd);
61-
map.put(slope, map.getOrDefault(slope, 0) + 1);
62-
}
8+
/**
9+
* credits: https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding
10+
*/
11+
public static class Solution1 {
12+
public int maxPoints(int[][] points) {
13+
if (points.length < 3) {
14+
return points.length;
15+
}
16+
int max = 0;
17+
Map<Long, Integer> map = new HashMap<>();
18+
for (int i = 0; i < points.length; i++) {
19+
int dup = 1;
20+
map.clear();
21+
for (int j = i + 1; j < points.length; j++) {
22+
int dx = points[j][0] - points[i][0];
23+
int dy = points[j][1] - points[i][1];
24+
if (dx == 0 && dy == 0) {
25+
dup++;
26+
} else {
27+
int gcd = getGcd(dx, dy);
28+
long slope = ((long) (dy / gcd) << 32) + (dx / gcd);
29+
map.put(slope, map.getOrDefault(slope, 0) + 1);
30+
}
31+
}
32+
max = Math.max(max, dup);
33+
for (Map.Entry<Long, Integer> entry : map.entrySet()) {
34+
max = Math.max(max, entry.getValue() + dup);
35+
}
36+
}
37+
return max;
6338
}
64-
max = Math.max(max, dup);
65-
for (Map.Entry<Long, Integer> entry : map.entrySet()) {
66-
max = Math.max(max, entry.getValue() + dup);
67-
}
68-
}
69-
return max;
70-
}
7139

72-
int getGcd(int a, int b) {
73-
return b == 0 ? a : getGcd(b, a % b);
40+
int getGcd(int a, int b) {
41+
return b == 0 ? a : getGcd(b, a % b);
42+
}
7443
}
75-
}
7644
}

0 commit comments

Comments
 (0)