-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path149.直线上最多的点数.ts
More file actions
89 lines (80 loc) · 1.65 KB
/
Copy path149.直线上最多的点数.ts
File metadata and controls
89 lines (80 loc) · 1.65 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* @lc app=leetcode.cn id=149 lang=typescript
*
* [149] 直线上最多的点数
*
* https://leetcode.cn/problems/max-points-on-a-line/description/
*
* algorithms
* Hard (38.01%)
* Likes: 436
* Dislikes: 0
* Total Accepted: 69.1K
* Total Submissions: 181.3K
* Testcase Example: '[[1,1],[2,2],[3,3]]'
*
* 给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。
*
*
*
* 示例 1:
*
*
* 输入:points = [[1,1],[2,2],[3,3]]
* 输出:3
*
*
* 示例 2:
*
*
* 输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
* 输出:4
*
*
*
*
* 提示:
*
*
* 1
* points[i].length == 2
* -10^4 i, yi
* points 中的所有点 互不相同
*
*
*/
export
// @lc code=start
function maxPoints(points: Point[]): number {
const lines: Record<string, Set<string>> = {}
for (let i = 0; i < points.length - 1; i++) {
const p1 = points[i]
for (let j = i + 1; j < points.length; j++) {
const p2 = points[j]
const hash = getLineHash(p1, p2)
if (!lines[hash]) {
lines[hash] = new Set<string>()
}
lines[hash].add(p1.join(','))
lines[hash].add(p2.join(','))
}
}
const retLines = Object.values(lines)
if (!retLines.length) {
return points.length
}
return Math.max(...retLines.map((x) => x.size))
}
type Point = [number, number]
function getLineHash(p1: Point, p2: Point): string {
const [x1, y1] = p1
const [x2, y2] = p2
const dy = y2 - y1, dx = x2 - x1
if (dx === 0) {
return String(x1)
}
const k = dy / dx
const b = y1 - k * x1
return [k, b].join('@')
}
// @lc code=end