-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmax-points-on-a-line.js
More file actions
39 lines (34 loc) · 871 Bytes
/
Copy pathmax-points-on-a-line.js
File metadata and controls
39 lines (34 loc) · 871 Bytes
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
/**
* @param {number[][]} points
* @return {number}
*/
var maxPoints = function(points) {
let map = new Map();
if(points.length=== 1){
return 1;
}
for(let i=0; i< points.length-1; i++){
for (let j= i+1; j< points.length; j++){
let x= points[i];
let y= points[j];
let s = slope(x,y);
let yInt = yIntercept(s, x);
let key = s+"_"+yInt
if(!map.has(key)){
map.set(key, new Set());
}
map.get(key).add(i);
map.get(key).add(j);
}
}
return Math.max(...[...map.values()].map(set=>set.size));
};
let slope = (x, y)=>{
return(y[1]-x[1])/(y[0]-x[0]);
}
let yIntercept = (slope, x)=>{
if(slope === Infinity || slope === -Infinity){
return x[0];
}
return x[1] - slope*x[0];
};