forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPointsOnStraightLine.java
More file actions
67 lines (51 loc) · 1.7 KB
/
MaxPointsOnStraightLine.java
File metadata and controls
67 lines (51 loc) · 1.7 KB
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
package Hashing;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Author - archit.s
* Date - 30/10/18
* Time - 11:55 PM
*/
public class MaxPointsOnStraightLine {
public int maxPoints(ArrayList<Integer> a, ArrayList<Integer> b) {
Map<Double, Integer> map = new HashMap<>();
int maxPoints = 0;
if(a==null || b==null || a.size() != b.size()){
return maxPoints;
}
for(int i=0;i<a.size();i++){
int overlapping = 1;
int vertical = 0;
for(int j=i+1;j<a.size();j++){
if(a.get(i).equals(a.get(j))){
if(b.get(i).equals(b.get(j))){
overlapping++;
}
else{
vertical++;
}
}
else{
double y = b.get(j) - b.get(i);
double x = a.get(j) - a.get(i);
double slope = 0.0;
if(!b.get(j).equals(b.get(i))){
slope = 1.0*(y/x);
}
map.put(slope, map.getOrDefault(slope,0)+1);
}
}
for(Double d : map.keySet()){
maxPoints = Math.max(maxPoints, map.get(d)+overlapping);
}
maxPoints = Math.max(maxPoints, vertical+overlapping);
map.clear();
}
return maxPoints;
}
public static void main(String[] args) {
System.out.println(new MaxPointsOnStraightLine().maxPoints(new ArrayList<>(Arrays.asList(4,8,-4)), new ArrayList<>(Arrays.asList(-4,-4,-4))));
}
}