Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3065d84

Browse files
authored
Update _149.java (fishercoder1534#74)
1 parent 9c593b9 commit 3065d84

File tree

1 file changed

+23
-88
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-88
lines changed
Lines changed: 23 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,30 @@
1-
package com.fishercoder.solutions;
1+
**Updated solution** [credits](https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding)
22

3-
import com.fishercoder.common.classes.Point;
4-
import java.util.HashMap;
5-
import java.util.Map;
6-
7-
/**
8-
* 149. Max Points on a Line
9-
*
10-
* Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
11-
*
12-
* Example 1:
13-
* Input: [[1,1],[2,2],[3,3]]
14-
* Output: 3
15-
* Explanation:
16-
* ^
17-
* |
18-
* | o
19-
* | o
20-
* | o
21-
* +------------->
22-
* 0 1 2 3 4
23-
*
24-
* Example 2:
25-
* Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
26-
* Output: 4
27-
* Explanation:
28-
* ^
29-
* |
30-
* | o
31-
* | o o
32-
* | o
33-
* | o o
34-
* +------------------->
35-
* 0 1 2 3 4 5 6
36-
*
37-
*/
38-
public class _149 {
39-
40-
public static class Solution1 {
41-
/** credit: https://leetcode.com/problems/max-points-on-a-line/discuss/47113/A-java-solution-with-notes */
42-
public int maxPoints(Point[] points) {
43-
if (points == null) {
44-
return 0;
45-
}
46-
if (points.length <= 2) {
47-
return points.length;
48-
}
49-
50-
Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
51-
int result = 0;
52-
for (int i = 0; i < points.length; i++) {
53-
map.clear();
54-
int overlap = 0;
3+
class Solution {
4+
public int maxPoints(int[][] points) {
5+
if(points.length < 3)return points.length;
556
int max = 0;
56-
for (int j = i + 1; j < points.length; j++) {
57-
int x = points[j].x - points[i].x;
58-
int y = points[j].y - points[i].y;
59-
if (x == 0 && y == 0) {
60-
overlap++;
61-
continue;
62-
}
63-
int gcd = generateGCD(x, y);
64-
if (gcd != 0) {
65-
x /= gcd;
66-
y /= gcd;
67-
}
68-
69-
if (map.containsKey(x)) {
70-
if (map.get(x).containsKey(y)) {
71-
map.get(x).put(y, map.get(x).get(y) + 1);
72-
} else {
73-
map.get(x).put(y, 1);
7+
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
8+
for(int i = 0;i < points.length;i++) {
9+
int dup = 1;
10+
map.clear();
11+
for(int j = i + 1;j < points.length;j++) {
12+
int dx = points[j][0] - points[i][0], dy = points[j][1] - points[i][1];
13+
if(dx == 0 && dy == 0)dup++;
14+
else {
15+
int gcd = getGcd(dx, dy);
16+
long slope = ((long)(dy / gcd) << 32) + (dx / gcd);
17+
map.put(slope, map.getOrDefault(slope, 0) + 1);
18+
}
7419
}
75-
} else {
76-
Map<Integer, Integer> m = new HashMap<>();
77-
m.put(y, 1);
78-
map.put(x, m);
79-
}
80-
max = Math.max(max, map.get(x).get(y));
20+
max = Math.max(max, dup);
21+
for(Map.Entry<Long, Integer> entry : map.entrySet())
22+
max = Math.max(max, entry.getValue() + dup);
8123
}
82-
result = Math.max(result, max + overlap + 1);
83-
}
84-
return result;
24+
return max;
8525
}
86-
87-
private int generateGCD(int a, int b) {
88-
if (b == 0) {
89-
return a;
90-
} else {
91-
return generateGCD(b, a % b);
92-
}
26+
27+
int getGcd(int a, int b) {
28+
return b == 0 ? a : getGcd(b, a % b);
9329
}
94-
}
9530
}

0 commit comments

Comments
 (0)