-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path149_max_points_on_a_line.py
More file actions
51 lines (41 loc) · 1.1 KB
/
149_max_points_on_a_line.py
File metadata and controls
51 lines (41 loc) · 1.1 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
# Definition for a point.
# class Point:
# def __init__(self, a=0, b=0):
# self.x = a
# self.y = b
class Solution:
def maxPoints(self, P):
"""
:type P: List[Point]
:rtype: int
"""
ans = 0
if not P:
return ans
n = len(P)
if n <= 2:
return n
for i in range(n):
S = {}
points = dups = 0
for j in range(i + 1, n):
dx = P[i].x - P[j].x
dy = P[i].y - P[j].y
if dx == 0 and dy == 0:
dups += 1
continue
gcd = self.get_gcd(dx, dy)
if gcd:
dx //= gcd
dy //= gcd
key = (dx, dy)
S[key] = S.get(key, 0) + 1
if S[key] > points:
points = S[key]
if points + dups + 1 > ans:
ans = points + dups + 1
return ans
def get_gcd(self, a, b):
if b == 0:
return a
return self.get_gcd(b, a % b)