class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
n = len(points)
if n <= 2:
return n
def get_slope(p1, p2):
if p1[0] == p2[0]:
return float("inf")
return (p2[1] - p1[1]) / (p2[0] - p1[0])
res = 1
for i in range(n):
for j in range(i + 1, n):
slope = get_slope(points[i], points[j])
cnt = 2
for k in range(j + 1, n):
if slope == get_slope(points[i], points[k]):
cnt += 1
res = max(res, cnt)
return resclass Solution:
def maxPoints(self, points: List[List[int]]) -> int:
res = 1
for i in range(len(points)):
p1 = points[i]
count = defaultdict(int)
for j in range(i + 1, len(points)):
p2 = points[j]
if p2[0] == p1[0]:
slope = float("inf")
else:
slope = (p2[1] - p1[1]) / (p2[0] - p1[0])
count[slope] += 1
res = max(res, count[slope] + 1)
return resclass Solution:
def maxPoints(self, points: List[List[int]]) -> int:
if len(points) <= 2:
return len(points)
def gcd(a, b):
return gcd(b, a % b) if b else a
res = 1
for i in range(len(points) - 1):
count = defaultdict(int)
for j in range(i + 1, len(points)):
dx = points[j][0] - points[i][0]
dy = points[j][1] - points[i][1]
g = gcd(dx, dy)
dx //= g
dy //= g
slope = (dx, dy)
count[slope] += 1
res = max(res, max(count.values()) + 1)
return resWhere is the number of points and is the maximum value in the points.