-
Notifications
You must be signed in to change notification settings - Fork 22
/
149. Max Points on a Line.java
executable file
·100 lines (87 loc) · 2.62 KB
/
149. Max Points on a Line.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
H
tags: Array, Hash Table, Geometry, Math
time: O(n^2)
space: O()
给list of (x,y) coordinates. Determine # of points on the same line
#### Observation
- If given n points, we can calculate all possible slopes. O(n^2) times
- For the two dots that generates the same slope, these dots could be on **parallel** slopes
- figure out how to prune the parallel dots
#### Trick: prune parallel dots using greatest common divider
- GCD: greatest common divider
- Devide the x and y by their greatest common divider, such that x and y can be reduced to minimum value
- All other x and y can be reduced to such condition as well
- track the final reduced (x,y) in a map: they are the key to the count
- No need to use Map<Integer, Map<Integer, Integer>> to perform 2 level mapping; just `map<String, Integer>`, where the key is "x@y"
```
/*
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Example 1:
Input: [[1,1],[2,2],[3,3]]
Output: 3
Explanation:
^
|
| o
| o
| o
+------------->
0 1 2 3 4
Example 2:
Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation:
^
|
| o
| o o
| o
| o o
+------------------->
0 1 2 3 4 5 6
*/
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
class Solution {
int X = 0;
int Y = 1;
public int maxPoints(int[][] points) {
if (points == null || points.length == 0) return 0;
int result = 0;
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < points.length; i++) { // for loop to try all points. O(n^2)
int max = 0, overlap = 0;
map.clear();
for (int j = i + 1; j < points.length; j++) {
int x = points[j][X] - points[i][X], y = points[j][Y] - points[i][Y];
if (x == 0 && y == 0) {
overlap++;
continue;
}
int gcd = findGCD(x, y);
if (gcd != 0) {
x /= gcd;
y /= gcd;
}
String key = x + "@" + y;
if (map.containsKey(key)) map.put(key, map.get(key) + 1);
else map.put(key, 1);
max = Math.max(max, map.get(key));
}
result = Math.max(result, max + overlap + 1); // # max num on certain slop + # of overlaop points + self
}
return result;
}
private int findGCD(int a, int b) {
if (b == 0) return a;
return findGCD(b, a % b);
}
}
```