File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } points
3
+ * @return {number }
4
+ * Time O(n^2)
5
+ * Space O(n)
6
+ */
7
+
8
+ function maxPoints ( points ) {
9
+ if ( points . length <= 2 ) return points . length ;
10
+
11
+ let maxCount = 0 ;
12
+ for ( let i = 0 ; i < points . length ; i ++ ) {
13
+ let slopeCount = { } ;
14
+ let samePoints = 1 ;
15
+ for ( let j = 0 ; j < points . length ; j ++ ) {
16
+ if ( i === j ) continue ;
17
+ if ( points [ i ] [ 0 ] === points [ j ] [ 0 ] && points [ i ] [ 1 ] === points [ j ] [ 1 ] ) {
18
+ samePoints ++ ;
19
+ continue ;
20
+ }
21
+ let slope = ( points [ i ] [ 1 ] - points [ j ] [ 1 ] ) / ( points [ i ] [ 0 ] - points [ j ] [ 0 ] ) ;
22
+ if ( slope in slopeCount ) {
23
+ slopeCount [ slope ] ++ ;
24
+ } else {
25
+ slopeCount [ slope ] = 1 ;
26
+ }
27
+ }
28
+ console . log ( slopeCount ) ;
29
+ let count = samePoints ;
30
+ for ( let slope in slopeCount ) {
31
+ count = Math . max ( count , slopeCount [ slope ] + samePoints ) ;
32
+ }
33
+ maxCount = Math . max ( maxCount , count ) ;
34
+ }
35
+
36
+ return maxCount ;
37
+ }
You can’t perform that action at this time.
0 commit comments