Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
# Definition for a point
# class Point:
# def __init__(self, a=0, b=0):
# self.x = a
# self.y = b
class Solution:
# @param points, a list of Points
# @return an integer
def maxPoints(self, points):
length = len(points)
occurance = 0
if length <= 2:
return length
slope = [[float('-inf') for dummy_row in range(length)] for dymmy_col in range(length)]
rcrd = []
for row in range(length):
cnt = 0
for col in range(length):
if col != row:
if (points[col].x, points[col].y) == (points[row].x, points[row].y):
cnt += 1
if points[col].x == points[row].x:
slope[row][col] = float('inf')
else:
slope[row][col] = (points[col].y - points[row].y) / \
float(points[col].x - points[row].x)
d = dict()
for item in slope[row]:
if item not in d.keys():
d[item] = 1
else:
d[item] += 1
if float('inf') in d.keys():
if max(d.values()) == d[float('inf')]:
rcrd.append(max(d.values()) + 1)
else:
rcrd.append(max(d.values()) + 1 + cnt)
else:
rcrd.append(max(d.values()) + 1 + cnt)
return max(rcrd)