0%

Leetcode149-maxPointsOnALine

Description

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

Example

Example 1:

1
2
3
4
5
6
7
8
9
10
Input: [[1,1],[2,2],[3,3]]
Output: 3
Explanation:
^
|
| o
| o
| o
+------------->
0 1 2 3 4

Example 2:
1
2
3
4
5
6
7
8
9
10
11
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

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

Solution

Solution 1: Hash Map

O(n^2), used (x1 - x2) / gcd and (y1 - y2) / gcd (gcd is the gcd between (x1 - x2) and (y1-y2))

To avoid to use double, which can occur accuracy problem

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
class Solution {
public int maxPoints(int[][] points) {
if (points == null) return 0;
if (points.length <= 2) return points.length;

Map<Integer, Map<Integer, Integer>> map = new HashMap<Integer, Map<Integer, Integer>>();
int result = 0;
for (int i = 0; i < points.length; i++) {
map.clear();
int num = 1, max = 0;
for (int j = i + 1; j < points.length; j++) {
int x = points[j][0] - points[i][0];
int y = points[j][1] - points[i][1];
if (x == 0 && y == 0) {
num++;
continue;
}
int gcd = generateGCD(x, y);
if (gcd != 0) {
x /= gcd;
y /= gcd;
}

if (map.containsKey(x)) {
map.get(x).put(y, map.get(x).getOrDefault(y, 0) + 1);
}
else{
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
m.put(y, 1);
map.put(x, m);
}
max = Math.max(max, map.get(x).get(y));
}
result = Math.max(result, max + num);
}
return result;
}

private int generateGCD(int a, int b) {
if (b == 0) return a;
else return generateGCD(b, a % b);
}
}

Solution 2

O(n^3), used Cross product to determine if these three points are in one line

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
class Solution {
public int maxPoints(int[][] points) {
int res = 0, n = points.length;
for (int i = 0; i < n; ++i) {
int duplicate = 1;
for (int j = i + 1; j < n; ++j) {
int cnt = 0;
long x1 = points[i][0], y1 = points[i][1];
long x2 = points[j][0], y2 = points[j][1];
if (x1 == x2 && y1 == y2) {
duplicate++;
continue;
}
for (int k = 0; k < n; ++k) {
int x3 = points[k][0], y3 = points[k][1];
if (x1*y2 + x2*y3 + x3*y1 - x3*y2 - x2*y1 - x1 * y3 == 0) {
cnt++;
}
}
res = Math.max(res, cnt);
}
res = Math.max(res, duplicate);
}
return res;
}
}