“””
Given n points on a 2D plane,
find the maximum number of points that lie on the same straight line.
“””
from decimal import Decimal
# Definition for a point.
class Point:
def __init__(self, a=0, b=0):
self.x = a
self.y = b
class Solution:
def maxPoints(self, points):
"""
:type points: List[Point]
:rtype: int
"""
if not points:
return 0
# xs=[point.x for point in points]
# ys=[point.y for point in points]
length=len(points)
if length==1:
return 1
counter=0
for ip in range(length-1):
kdict={`i`:1}
same=0
for jp in range(ip+1,length):
if points[jp].x==points[ip].x and points[ip].y==points[jp].y:
same+=1
elif points[jp].x==points[ip].x:
kdict[`i`]+=1
else:
k=Decimal(points[jp].y-points[ip].y*Decimal(1.0))/Decimal(points[jp].x-points[ip].x)
print(k)
if k not in kdict.keys():
kdict[k]=2
else:
kdict[k]+=1
counter=max(counter,max(kdict.values())+same)
# print(counter)
return counter
if __name__==`__main__`:
st=Solution()
points=[Point(i,j) for i in range(9) for j in range(3,12)]
points=[Point(1,2),Point(1,2),Point(1,2),Point(3,6),Point(2,4),Point(4,8),Point(5,10),]
# points=[Point(0,0)]
# points=[Point(0,0),Point(1,0)]
points=[Point(0,0),Point(1,1),Point(1,-1)]
points=[Point(0,0),Point(94911151,94911150),Point(94911152,94911151)]
out=st.maxPoints(points)
print(out)
float```