File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 88
88
143|[ Reorder List] ( ./0143-reorder-list.js ) |Medium|
89
89
144|[ Binary Tree Preorder Traversal] ( ./0144-binary-tree-preorder-traversal.js ) |Easy|
90
90
145|[ Binary Tree Postorder Traversal] ( ./0145-binary-tree-postorder-traversal.js ) |Easy|
91
+ 149|[ Max Points on a Line] ( ./0149-max-points-on-a-line.js ) |Hard|
91
92
151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
92
93
152|[ Maximum Product Subarray] ( ./0152-maximum-product-subarray.js ) |Medium|
93
94
167|[ Two Sum II - Input Array Is Sorted] ( ./0167-two-sum-ii-input-array-is-sorted.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 149. Max Points on a Line
3
+ * https://leetcode.com/problems/max-points-on-a-line/
4
+ * Difficulty: Hard
5
+ *
6
+ * Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane,
7
+ * return the maximum number of points that lie on the same straight line.
8
+ */
9
+
10
+ /**
11
+ * @param {number[][] } points
12
+ * @return {number }
13
+ */
14
+ var maxPoints = function ( points ) {
15
+ let max = 0 ;
16
+
17
+ points . forEach ( x => {
18
+ const slopes = new Map ( ) ;
19
+
20
+ points . forEach ( y => {
21
+ if ( x === y ) return ;
22
+ const slope = y [ 0 ] - x [ 0 ] !== 0 ? ( y [ 1 ] - x [ 1 ] ) / ( y [ 0 ] - x [ 0 ] ) : Infinity ;
23
+ slopes . set ( slope , ( slopes . get ( slope ) || 0 ) + 1 ) ;
24
+ max = Math . max ( max , slopes . get ( slope ) ) ;
25
+ } ) ;
26
+ } ) ;
27
+
28
+ return max + 1 ;
29
+ } ;
You can’t perform that action at this time.
0 commit comments