-
Notifications
You must be signed in to change notification settings - Fork 0
/
0149-max-points-on-a-line.py
46 lines (36 loc) · 1.18 KB
/
0149-max-points-on-a-line.py
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
class Solution:
# O(1) time and O(1) space
# line slope: https://www.mathsisfun.com/algebra/line-equation-2points.html
def calculateNormalizedSlope(self, a, b):
run = a[0] - b[0]
if run == 0:
return (1, 0)
if run < 0:
a, b = b, a
run = a[0] - b[0]
rise = a[1] - b[1]
_gcd = gcd(abs(rise), run)
return (
rise // _gcd,
run // _gcd
)
# O(n ^ 2) time and O(n) space
def maxPoints(self, points: List[List[int]]) -> int:
if len(points) < 3:
return len(points)
maximum = 0
for i in range(0, len(points) - 1):
a = tuple(points[i])
counts: DefaultDict[[int, int], int] = {}
for j in range(i + 1, len(points)):
b = tuple(points[j])
slope = self.calculateNormalizedSlope(a, b)
if slope not in counts:
counts[slope] = 2
else:
counts[slope] += 1
maximum = max(
maximum,
max(counts.values()),
)
return maximum