216 - 149 直线上最多的点数
题目
解答
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
from collections import defaultdict
def clac(points, i):
hashmap = defaultdict(int)
same = 0
for j in range(0, len(points)):
if j != i:
if points[i] == points[j]:
same += 1
continue
if points[i][1] - points[j][1] == 0:
k = float('inf')
else:
k = (points[i][0] - points[j][0]) / (points[i][1] -
points[j][1])
hashmap[k] += 1
return 1 + same + (max(hashmap.values()) if hashmap else 0)
res = 0
if len(points) == 1:
return 1
for i in range(len(points)):
res = max(res, clac(points, i))
return resLast updated