Skip to content

Commit be1db37

Browse files
committedJan 8, 2023
Add solution #149
1 parent 4548013 commit be1db37

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
143|[Reorder List](./0143-reorder-list.js)|Medium|
8989
144|[Binary Tree Preorder Traversal](./0144-binary-tree-preorder-traversal.js)|Easy|
9090
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|
9192
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
9293
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
9394
167|[Two Sum II - Input Array Is Sorted](./0167-two-sum-ii-input-array-is-sorted.js)|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.