Max Points on a Line LeetCode solution in Python

1
class Solution:
2
def maxPoints(self, points: List[List[int]]) -> int:
3
ans = 0
4
n = len(points)
5

6
for i in range(n):
7
d = collections.defaultdict(int)
8
for j in range(n):
9
if i != j:
10
slope = float("inf")
11
if points[j][1] - points[i][1] != 0:
12
slope = (points[j][0] - points[i][0]) / (
13
points[j][1] - points[i][1]
14
)
15
d[slope] += 1
16
if d:
17
ans = max(ans, max(d.values()) + 1)
18
else:
19
ans = max(ans, 1)
20

21
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0