Accepted recursive python solution


  • 0

    This solution just counts how many pairs of points give the same "point-slope" values.

    # Definition for a point.
    # class Point(object):
    #     def __init__(self, a=0, b=0):
    #         self.x = a
    #         self.y = b
    
    class Solution(object):
        def maxPoints(self, points):
            """
            :type points: List[Point]
            :rtype: int
            """
            
            if len(points)==0: return 0
            
            (x0, y0) = map(float,(points[0].x, points[0].y))
            
            count  = dict({None:0})
            copies = 1
            for p in points[1:]:
                (x1, y1) = map(float,(p.x,p.y))
                
                if (x0,y0) == (x1,y1):
                    copies += 1
                    continue
                
                if y0-y1 == 0:
                    m = float('inf')
                else:
                    m = (x0-x1)/(y0-y1)
                
                b = y0-m*x0
                
                if (m,b) in count:
                    count[(m,b)]+=1
                else:
                    count[(m,b)]=1
                    
            return max( copies+max( count[x] for x in count ), self.maxPoints(points[1:]) )
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.