# Definition for a point. # class Point: # def __init__(self, a=0, b=0): # self.x = a # self.y = b
classSolution: defgcd(self, target): if target[0] == 0or target[1] == 0: return1 left =abs(target[1]) right =abs(target[0]) while right % left != 0: temp = left left = right % left right = temp sign = 1if target[0] > 0else-1 return left * sign defmaxPoints(self, points): """ :type points: List[Point] :rtype: int """ if points isNoneor len(points) == 0: return0 if len(points) == 1: return1 points_gradient = [{} for i in range(len(points))] counter = [1for i in range(len(points))] for i in range(len(points) - 1): for j in range(i + 1, len(points)): if (points[i].x == points[j].x and points[i].y == points[j].y): counter[i] += 1 counter[j] += 1 continue raw_gradient = None if points[i].x == points[j].x: raw_gradient = (1, 0) elif points[i].y == points[j].y: raw_gradient = (0, 1) else: raw_gradient = ((points[i].y - points[j].y), (points[i].x - points[j].x)) gradient = (raw_gradient[0] / self.gcd(raw_gradient), raw_gradient[1] / self.gcd(raw_gradient)) if points_gradient[i].get(gradient) isNone: points_gradient[i][gradient] = 1 else: points_gradient[i][gradient] += 1 if points_gradient[j].get(gradient) isNone: points_gradient[j][gradient] = 1 else: points_gradient[j][gradient] += 1 ret = -1 print(points_gradient) for (index, item) in enumerate(points_gradient): ret = max(ret, (max(item.values()) if item else0) + counter[index]) return ret